Search code examples
phpxpathwsdlsimplexml

PHP Xpath: How to get the action URL from any method on a WSDL?


I am trying to recover the action URL from any arbitrary WSDL using only the action (and wsdl) desired:

$method = "consultarProcesso";
$wsdl = "https://webserverseguro.tjrj.jus.br/MNI/Servico.svc?wsdl";
$xmlWSDL = new SimpleXMLElement(file_get_contents($wsdl));
$xpath = "//*[local-name()='operation'][@name='$method']";
$result = $xmlWSDL->xpath($xpath);
var_dump($result[0]);

The problem is that I don't know how to get the node values from $result[0] to recover the desired value in this example:

http://www.cnj.jus.br/servico-intercomunicacao-2.2.2/consultarProcesso

What can I do to achieve that?


Solution

  • There are a couple of ways to find the input element of interest directly using xpath. You can use the local-name() as you are currently:

    $xpath = "//*[local-name()='operation'][@name='$method']/*[local-name()='input']";
    

    or directly specify the namespace in the xpath:

    $xpath = "//wsdl:operation[@name='$method']/wsdl:input";
    

    Once you have the desired element, you can look through its namespaced attributes for the Action:

    $result = $xmlWSDL->xpath($xpath)[0];
    $namespaces = $result->getNameSpaces();
    foreach ($namespaces as $ns) {
        if (isset($result->attributes($ns)['Action'])) $url = (string)$result->attributes($ns)['Action'];
    }
    echo $url;
    

    Output:

    http://www.cnj.jus.br/servico-intercomunicacao-2.2.2/consultarProcesso
    

    Demo on 3v4l.org