繁体   English   中英

用CURL在Php中使用Soap客户端请求不起作用

[英]Soap client request in Php with CURL not working

我如何创建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);

您不应发布到该URL。 那不是服务端点,而是WSDL,它定义了所提供的操作。

PHP SoapClient类使您可以轻松构建肥皂请求:

$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>所定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM