2013-07-04 171 views
4

我是肥皂的初學者,我如何發送soap請求?我在谷歌搜索和嘗試不同的方法,但可悲的是它沒有爲我工作。如何發送curl中的SOAP請求

我真的很感謝你的幫助。

這裏,我應該送樣要求:

POST /Universal/WebService.asmx HTTP/1.1 
Host: www.sample.com 
Content-Type: text/xml;charset="utf-8" 
Content-Length: length 
SOAPAction: https://www.sample.com/Universal/WebService.asmx/Request 

<?xml version=\"1.0\" encoding=\"utf-8\"?> 
<soap:Envelope xmlns:xsi="http://wwww.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<Request xmlns="https://www.sample.com/Universal/WebService.asmx"> 
<ID>CPHK01</ID> 
<UID>TEST</UID> 
<PWD>TEST</PWD> 
<target_mpn>09183530925</target_mpn> 
<amount>115</amount> 
<ref_no>20060830143030112</ref_no> 
<source_mpn>67720014</source_mpn> 
</Request> 
</soap:Body> 
</soap:Envelope> 

這裏的響應:

HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 

<?xml version = "1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://wwww.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<RequestResponse xmlns="https://www.sample.com/Universal/WebService.asmx"> 
<RequestResult> 
<Ref_no>20060830143030112</Ref_no> 
<StatusCode>101</StatusCode> 
</RequestResult> 
</RequestResponse> 
</soap:Body> 
</soap:Envelope> 
+3

我對'SOAP'的主要建議是**不要丟棄!**。 * [抱歉,無法阻止拖動:)。] * – CodeAngry

+0

[PHP的CURL中的SOAP請求]的可能重複(http://stackoverflow.com/questions/7120586/soap-request-in-php-with-curl ) - 和其他一些。在提問前請使用搜索。 – hakre

+0

@hakre **喜歡**放棄'SOAP' – Jimbo

回答

3

PHP提供了一個原生SoapClient類。構造函數將WSDL作爲參數。這比cURL更受歡迎,因爲SoapClient處理所有的SOAP錯綜複雜的問題,它允許您處理本地對象和數組,並且無需手動構建SOAP信封和XML。

try { 
    $client = new SoapClient('https://www.sample.com/Universal/WebService.asmx?wsdl'); 
    $response = $client->Request(array(
     'ID' => 'xxxx', 
     'UID' => 'xxxx', 
     'PWD' => 'xxxx', 
     'target_mpn' => 'xxxx', 
     'amount' => 'xxxx', 
     'ref_no' => 'xxxx', 
     'source_mpn' => 'xxxx' 
    )); 

    print_r($response); // view the full response to see what is returned 

    // or get the response properties: 
    echo $response->RequestResult->Ref_no; 
    echo $response->RequestResult->StatusCode; 

} catch (Exception $e) { 
    echo $e->getMessage(); 
} 
+0

謝謝MrCode我會給你一個這樣的鏡頭。 – lgDroidZ

+0

我收到了這個錯誤McCode:SOAP-ERROR:解析WSDL:無法從 – lgDroidZ

+0

加載您是否更新了代碼中的wsdl?在瀏覽器中檢查它是否可以訪問。 – MrCode