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?
这有效,但我想知道是否有一种方法可以使用PHP的SOAP扩展本地获取它?
I was thinking something like the following would work but it doesn't:
我认为以下内容会起作用,但事实并非如此:
$client = new SoapClient($this->wsdl);
$version = $client->eBayAPIInterfaceService->Version;
2 个解决方案
#1
4
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.
使用常规SoapClient无法做到你想要的。最好的办法是扩展SoapClient类并抽象出这个要求以获得版本。
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.
请注意,file_get_contents未缓存,因此它将始终加载WSDL文件。另一方面,SoapClient缓存WSDL,因此您必须自己处理它。
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 ;) )
也许看看NuSOAP。您将能够修改代码以适合您的目的,而无需加载WSDL两次(当然您也可以修改SoapClient但这是另一个冠军;))
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());
#2
0
Have you tried simplexml_load_file? Worked for me when i needed to parse an XML-File with php.
你试过simplexml_load_file吗?当我需要使用php解析XML文件时,为我工作。
<?php
$file = "/path/to/yourfile.wsdl";
$xml = simplexml_load_file($file) or die ("Error while loading: ".$file."\n");
echo $xml->service->documentation->Version;
//if there are more Service-Elements access them via index
echo $xml->service[index]->documentation->Version;
//...where index in the number of the service appearing
//if you count them from top to buttom. So if "eBayAPIInterfaceService"
//is the third service-Element
echo $xml->service[2]->documentation->Version;
?>
#1
4
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.
使用常规SoapClient无法做到你想要的。最好的办法是扩展SoapClient类并抽象出这个要求以获得版本。
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.
请注意,file_get_contents未缓存,因此它将始终加载WSDL文件。另一方面,SoapClient缓存WSDL,因此您必须自己处理它。
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 ;) )
也许看看NuSOAP。您将能够修改代码以适合您的目的,而无需加载WSDL两次(当然您也可以修改SoapClient但这是另一个冠军;))
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());
#2
0
Have you tried simplexml_load_file? Worked for me when i needed to parse an XML-File with php.
你试过simplexml_load_file吗?当我需要使用php解析XML文件时,为我工作。
<?php
$file = "/path/to/yourfile.wsdl";
$xml = simplexml_load_file($file) or die ("Error while loading: ".$file."\n");
echo $xml->service->documentation->Version;
//if there are more Service-Elements access them via index
echo $xml->service[index]->documentation->Version;
//...where index in the number of the service appearing
//if you count them from top to buttom. So if "eBayAPIInterfaceService"
//is the third service-Element
echo $xml->service[2]->documentation->Version;
?>