Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element from WSDL in PHP using SoapClient

I'd like to get the text from the <Version> element which is nested inside the <service> block of a WSDL. The WSDL in question is Ebay's Trading api. The snippet in question looks something like this:

<wsdl:service name="eBayAPIInterfaceService">
    <wsdl:documentation>
        <Version>941</Version>
    </wsdl:documentation>
    <wsdl:port binding="ns:eBayAPISoapBinding" name="eBayAPI">
        <wsdlsoap:address location="https://api.ebay.com/wsapi"/>
    </wsdl:port>
</wsdl:service>

I'm currently doing this:

$xml = new DOMDocument();
$xml->load($this->wsdl);
$version = $xml->getElementsByTagName('Version')->item(0)->nodeValue;

This works but I'm wondering if there is a method to get this natively using PHP's SOAP extension?

I was thinking something like the following would work but it doesn't:

$client = new SoapClient($this->wsdl);
$version = $client->eBayAPIInterfaceService->Version;
like image 383
Eaten by a Grue Avatar asked Oct 13 '15 18:10

Eaten by a Grue


1 Answers

It is not possible to do what you want with the regular SoapClient. Your best bet is to extend the SoapClient class and abstract-away this requirement to get the version.

Please beware that file_get_contents is not cached so it will always load the WSDL file. On the other hand SoapClient caches the WSDL so you will have to deal with it yourself.

Perhaps look into NuSOAP. You will be able to modify the code to suit your purposes without loading the WSDL twice (of course you are able to modify SoapClient too but that's another championship ;) )

namespace Application;

use DOMDocument;

class SoapClient extends \SoapClient {
    private $version = null;

    function __construct($wsdl, $options = array()) {
        $data = file_get_contents($wsdl);

        $xml = new DOMDocument();
        $xml->loadXML($data);
        $this->version = $xml->getElementsByTagName('Version')->item(0)->nodeValue;

        // or just use $wsdl :P
        // this is just to reuse the already loaded WSDL
        $data = "data://text/plain;base64,".base64_encode($data);
        parent::__construct($data, $options);
    }

    public function getVersion() {
        return is_null($this->version) ? "Uknown" : $this->version;
    }
}

$client = new SoapClient("http://developer.ebay.com/webservices/latest/ebaysvc.wsdl");
var_dump($client->getVersion());
like image 76
Ricardo Velhote Avatar answered Sep 20 '22 11:09

Ricardo Velhote