2016-08-03 80 views
0

如何創建php soap請求,看起來像這樣。我無法在Php curl中獲得回覆。但是小提琴手的網絡調試器在代碼上工作得很好。在Php中使用CURL不起作用的肥皂客戶端請求

這裏是原始請求:

POST http://195.230.180.189:8280/services/TopupService?wsdl HTTP/0.9 主機:195.230.180.189:8280 的Content-Length:691

<?xml version="1.0"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:top="http://www.inew-cs.com/mvno/integration/TopupService/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
    <top:TopupRequest> 
     <amount>1</amount> 
     <amountUnitRelation>1</amountUnitRelation> 
     <subscriber> 
     <msisdn>8801701340002</msisdn> 
     </subscriber> 
     <source> 
     <distributorId>PayWell</distributorId> 
     </source> 
     <referenceId>12345</referenceId> 
     <transactionId>09876543</transactionId> 
    </top:TopupRequest> 
    </soapenv:Body> 
    </soapenv:Envelope> 

在PHP捲曲請求:

$url="http://195.230.180.189:8280/services/TopupService?wsdl"; 
    $xml='<?xml version="1.0"?> 
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:top="http://www.inew-cs.com/mvno/integration/TopupService/"> 
     <soapenv:Header/> 
     <soapenv:Body> 
     <top:TopupRequest> 
     <amount>1</amount> 
     <amountUnitRelation>1</amountUnitRelation> 
     <subscriber> 
      <msisdn>8801701340002</msisdn> 
     </subscriber> 
     <source> 
      <distributorId>100</distributorId> 
     </source> 
     <referenceId>12345</referenceId> 
     <transactionId>09876543</transactionId> 
     </top:TopupRequest> 
     </soapenv:Body> 
     </soapenv:Envelope>'; 
    $headers = array(
     "Content-type: text/xml", 
     "Content-length: " . strlen($xml), 
     "Connection: close" 
    ); 
    $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$url); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 

    echo $result = curl_exec($ch); 
    if(curl_exec($ch) === false) 
    { 
     echo 'Curl error: ' . curl_error($ch); 
    } 
    else 
    { 
     //echo 'Operation completed without any errors'; 
    } 
    // Close handle 
    curl_close($ch); 
+1

http://php.net/manual/en/class.soapclient.php –

回答

1

您不應該發佈到該URL。這不是服務端點,它是定義提供的操作的WSDL。

PHP SoapClient的類可以讓您輕鬆構建SOAP請求:

$wsdl = 'http://195.230.180.189:8280/services/TopupService?wsdl'; 

$options = [ 
    'trace' => true, 
    'cache' => WSDL_CACHE_NONE, 
    'exceptions' => true 
]; 

$client = new SoapClient($wsdl, $options); 

$payload = [ 
    'amount' => 1, 
    'amountUnitRelation' => 1, 
    'subscriber' => [ 
     'msisdn' => '8801701340002' 
    ], 
    'source' => [ 
     'distributorId' => 'PayWell' 
    ], 
    'referenceId' => '12345', 
    'transactionId' => '09876543', 
]; 

$response = $client->topup($payload); 

解析給定的WSDL後,$client現在有方法topup,由<wsdl:operation>定義。

+0

難道你的意思是嘗試這種方式 $選項=陣列( '跟蹤'=>真實, '緩存'=> WSDL_CACHE_NONE , 'exceptions'=> true ); $ client = new SoapClient($ wsdl,$ options); $有效載荷=陣列( '量'=> 1, 'amountUnitRelation'=> 1, '訂戶'=>數組( 'MSISDN'=> '8801701340002' ) '源'=>數組( 'distributorId'=>'PayWell' ), 'referenceId'=>'12345', 'transactionId'=>'09876543' ); print $ response = $ client-> topup($ payload); – Ipshita

+0

我試過你的建議方式,但沒有奏效。當我嘗試上述方式時,發生致命錯誤:Uncaught SoapFault異常:[HTTP]無法連接到/var/www/html/pwl/paywell_crons/gp_plus_api_test.php:142中的主機堆棧跟蹤:#0 [內部函數] :SoapClient - > __ doRequest('__ call('topup',Array)#2 /var/www/html/pwl/paywell_crons/gp_plus_api_test.php(142):SoapClient-> topup(Array)#3 {main}拋出/無功/ www/html等/ PWL/paywell_crons/gp_plus_api_test.php – Ipshita