Php中的肥皂客户端请求,CURL无法正常工作

时间:2016-08-03 07:26:22

标签: php curl soap

如何创建php soap请求看起来像这样。我无法得到Php curl的回复。但是fiddler web调试器在代码上运行得非常好。

这是原始请求:

POST http://195.230.180.189:8280/services/TopupService?wsdl HTTP / 0.9 主持人:195.230.180.189:8280 内容长度: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 curl请求中:

$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 个答案:

答案 0 :(得分:1)

您不应发布到该网址。这不是服务端点,而是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>