2016-08-03 96 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