简体   繁体   中英

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?

You can retrieve this info using the namespace arguments of both SimpleElement::children and SimpleElement::attributes :

// Retrieve the `wsdl:` namespaced children of the operation
[$input, $output] = $result[0]->children('wsdl', true);

// Retrieve the `wsaw:`-namespaced attributes of the input element,
// then grab the one named Action
$actionAttribute = $input->attributes('wsaw', true)->Action;

// Convert its value into a string
$actionUrl = (string)$actionAttribute;

(Obviously this is over-commented for the purpose of this answer.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM