簡體   English   中英

使用php SOAP和WSDL的正確步驟是什么?

[英]What are the right steps to work with php SOAP and WSDL?

我已經解決了許多問題和困惑,請參閱

最終守則和關於這個問題的問題


在開發PHP SOAP服務時(第一次),我面臨很多錯誤。

讓我繪制一個場景,以便更好地理解我的理解

我手上有什么

  • 網址( <weburl>.asmx
  • 2個WSDL鏈接
    • <wsdlink> ==> https:... / Schemas / Informationservice.WSDL
    • <other_wsdlink> ==> https:... / Schemas / Orderuploadservice.WSDL
  • 3個證書文件( <cert>.pem<cert>.CER<cert>.PFX

PHP SOAP文檔的參考資料中,我創建了這個

PS這個Web服務在ASP.netSOAP 1.1上,所以用http s運行它

下面是主要代碼部分

  $x = new SoapClient(<wsdlink>, [
                                // 'local_cert' => '<cert>.pem',
                                // 'allow_self_signed' => true,
                                // 'cache_wsdl' => WSDL_CACHE_NONE,
                                'location' =>'https://192.168.xx.xx/wsdl/try.php',
                                'uri' =><other_wsdlink>,
                                'passphrase' => $passphrase,
                                'exceptions' => false,
                                'trace' => true,
                                'soap_version' => SOAP_1_1
                                //'connection_timeout'=>2000,
                                //'encoding'=> 'ISO-8859-1' // 'UTF-8'
                            ]);

這是2個案例


1.當我運行它(評論3行)時,它需要太多時間來響應並最終以XML形式給出錯誤,

  • 過程'requestedMethodRequest'不存在。
  • 函數'requestedMethod'不存在。

看到輸出

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring>Procedure 'requestedMethodRequest' not present</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

或者有時候(通過評論其他幾個選項或對其進行評論)

<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
    <SOAP-ENV:Fault>
        <faultcode>SOAP-ENV:Server</faultcode>
        <faultstring>Function 'requestedMethod' doesn't exist</faultstring>
    </SOAP-ENV:Fault>
</SOAP-ENV:Body></SOAP-ENV:Envelope>

盡管函數存在於返回SoapServer::getFunctions()

Array
(
[0] => firstFunctionWithoutParam
[1] => secondFunctionWithoutParam
[2] => requestedMethodResponse RequestedMethod(requestedMethodRequest $messagePart)
[3] => secondFunctionClassParam
)

2.當我取消評論3行以上時,會出現多個警告和致命錯誤

警告:SoapClient :: SoapClient():

  - Unable to set private key file <cert>.pem - failed to create an SSL handle - Failed to enable crypto - (<wsdlink>):failed to open stream: operation failed - I/O warning : failed to load external entity <wsdlink> 

致命錯誤:SOAP-ERROR:解析WSDL:無法從<wsdlink>加載:無法加載外部實體<wsdlink>

我搜索並閱讀各種博客的所有錯誤並應用他們的建議,但到目前為止沒有運氣。 這個博客之一 ,我刪除了/tmp/wsdl*任何臨時文件,並使用ini_set禁用了wsdl緩存

    ini_set('soap.wsdl_cache_enabled',0);
    ini_set('soap.wsdl_cache_ttl',0);

我的疑問和困惑是:

  1. 我是否需要在構造函數中設置soapClientsoapServer

  2. 我是否需要為證書文件設置一些額外的規則?

  3. $uri選項做什么(是強制性的)以及為請求發送的內容

  4. 這是證書問題還是可能是系統或網絡問題( UBUNTU 14.04 ),如果是,那么如何檢查?

更新

更改許可證書

 openssl pkcs12 -in certificatefile.PFX -out certfile.pem -nodes -clcerts  

並取消注釋cert選項並注釋__doRequest方法中的所有preg_replace代碼

但現在提出了Header問題 我注意到我發送的請求正在被修剪或刪除。

我通過SoapClient::__soapCall發送的請求是

<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
          <soap:Body>
            <addressVerificationRequest xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common">
              <addressVerification>
                <samId xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common">string</samId>
                ...
                <postalCode xmlns="urn:DCNZ.MSP.Wireline.Wholesale.Common">string</postalCode>
              </addressVerification>
            </addressVerificationRequest>
          </soap:Body>
        </soap:Envelope>;

var_dump($client->__getLastRequest());

<?xml version="1.0" encoding="UTF-8"?>  
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:DCNZ.MSP.Wireline.Wholesale.Messages">  
 <SOAP-ENV:Body><ns1:addressVerificationRequest/></SOAP-ENV:Body>
</SOAP-ENV:Envelope>'

上面的標題問題已修復,它是生成錯誤的uri值的原因。 它將是命名空間

確保在PHP中獲得正確類的任何簡單方法是使用https://github.com/wsdl2phpgenerator/wsdl2phpgenerator,它們提供了一個命令行生成器,您可以使用您擁有的WSDL運行它,它將生成一堆所有請求和響應的PHP類,最重要的是服務。

從您調用Web服務的類中,您可以執行以下操作:

// Construct the service
$service = new Service();
// Construct the request
$request = new RequestMethod();
$request->element = "test";
// Call the function
$response = $service->callRequest($request);
// process response
die(var_dump($response));

如果您正在使用類似框架的東西,您可能必須重構生成的類,但它為您提供了一個很好的起點。

暫無
暫無

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

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