2015-10-13 146 views
12

我想从嵌套在WSDL的<service>块中的<Version>元素中获取文本。有问题的WSDL是Ebay的Trading api。有问题的片段看起来是这样的:使用SoapClient从PHP获取元素使用SoapClient获取PHP中的元素

<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> 

目前,我正在做这个:

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

这工作,但我不知道是否有得到这个本地使用PHP的SOAP扩展的方法?

我的想法像下面这样的工作,但它并不:

$client = new SoapClient($this->wsdl); 
$version = $client->eBayAPIInterfaceService->Version; 
+0

我认为只发布一个链接作为一个答案是不好的形式,所以我反而评论。 我发现这个链接在学习如何使用SoapClient PHP类时非常有用,它提供了使用WSDL的例子。该类将数据作为可以从中获取数据的对象返回。 – crdunst

+0

@crdunst - 我没有看到任何方式从SoapClient类中获取该元素。我可以初始化客户端,调用方法,获取属性等,但对于我的生活,我无法弄清楚如何访问''。 wsdl在这里公开可用http://developer.ebay.com/webservices/latest/ebaysvc.wsdl。如果你可以提供一个使用SoapClient的工作示例,这将是非常有用的。 – billynoah

+0

我开始为你着想,但ebay API似乎比我一直在使用的API复杂得多。我发现这个答案虽然 - 它似乎有一个工作的例子:http://stackoverflow.com/questions/16502207/how-to-connect-to-the-ebay-trading-api-through-soapclient祝你好运。 – crdunst

回答

4

这是不可能做你想要与正规SoapClient什么。你最好的选择是扩展SoapClient类并抽象出这个需求来获得版本。

请注意,file_get_contents未被缓存,因此它将始终加载WSDL文件。另一方面SoapClient缓存WSDL,所以你将不得不自己处理它。

也许看看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()); 
+0

这与理想情况相差甚远,比我已经做的事情要轻1000倍左右。但是,感谢所有提供一个想法。 – billynoah

0

您是否尝试过使用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; 



?> 
+0

这个问题特别涉及PHP SOAP客户端,所以这个答案是不相关的,也是多余的,因为我已经演示了类似的东西,使用DOM Document可以正常工作。我修改了标题以使其更清楚。 – billynoah