簡體   English   中英

使用SoapClient從PHP中獲取WSDL元素

[英]Get element from WSDL in PHP using SoapClient

我想從<Version>元素中獲取文本,該元素嵌套在WSDL的<service>塊中。 有問題的WSDL是Ebay的交易 api。 有問題的片段看起來像這樣:

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

使用常規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());

你試過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;



?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM