WithCoderWithCoderWithCoder

PHP使用Curl调用https协议的WebService

    因为业务需要,需要将数据通过接口推送给第三方公司。开发语言是PHP,第三方公司提供的接口为Webservice。刚开始使用的是http协议,使用PHP的SoapClient将推送业务开发完成后一直运行正常。最近两天收到对方通知,说接口改为https协议,看了下推送日志,最近几天的推送都报错了。

    刚开始想着简单的将调用接口的http协议修改为https就行了,测试之后,程序报错。使用很多方法,调用接口一直出错,不是连不上host,就是调用不成功。因为时间紧张,所以考虑使用Curl进行提交XML,获取信息响应,解析Response XML。

    具体代码如下:

<?php
// 构造请求数据
$data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:OperationMethod>
         <!--Optional:-->
         <tem:operationType>UPDATESWBHZDATA</tem:operationType>
         <!--Optional:-->
         <tem:jsonObj>eyJVa2V5IjoiM2ExMzgyY2I2ZGIwNjBiMzBlOGUxZjJhNjdlOWRlYjAiLCJJc1F1YWxpZmllZCI6MSwiQmFvQ3VuU2hpSmlhbiI6IjIwMjAtMDYtMjQgMTA6MzA6MzUiLCJTaGVCZWlCaWFuSGFvIjoiU1NYTS1TUzAxLVNXMSIsIlNqZ2wxIjowLCJTamdsMiI6MS43NjIsIlNqZ2wzIjoyLjcwNiwiU2pnbDQiOjIuOTc1LCJTamdsNSI6Mi4yOTksIlNqZ2w2IjoxLjIxMiwiU2pmbDEiOjAuNTU3LCJTamZsMiI6MCwiU2pzaHVpIjowLCJTaGlqaWFuUyI6IjIwMjAtMDYtMjQgMTA6MjU6MzUiLCJTaGlqaWFuRSI6IjIwMjAtMDYtMjQgMTA6MzA6MzUiLCJHbGNoYW5nbGlhbmciOjExLjUxMSwiTGxnbDEiOjAsIkxsZ2wyIjoxLjk2LCJMbGdsMyI6Mi44OCwiTGxnbDQiOjMuMTEsIkxsZ2w1IjoyLjE5LCJMbGdsNiI6MS4zOCwiTGxmbDEiOjAuNDMsIkxsZmwyIjowLCJMbHNodWkiOjAsIlBlcmdsMSI6MCwiUGVyZ2wyIjoxNS4zMSwiUGVyZ2wzIjoyMy41MSwiUGVyZ2w0IjoyNS44NCwiUGVyZ2w1IjoxOS45NywiUGVyZ2w2IjoxMC41MywiUGVyZmwxIjo0Ljg0LCJQZXJmbDIiOjAsIlBlcnNodWkiOjAsIkJlaXkxIjoiXHU1OTA3XHU3NTI4MSIsIkJlaXkyIjoiXHU1OTA3XHU3NTI4MiIsIkJlaXkzIjoiXHU1OTA3XHU3NTI4MyIsIkNhaWppc2hpamlhbiI6IjIwMjAtMDYtMjQgMTA6MzA6MzYiLCJJc2RvIjowLCJTamdsc2YxIjowLCJTamdsc2YyIjowLCJTamdsc2YzIjowLCJTamdsc2Y0IjowLCJTamdsc2Y1IjowLCJTamdsc2Y2IjowLCJTamZsc2YxIjowLCJTamZsc2YyIjowLCJQZWlmYW5naGFvIjoiMSIsIlBlaWZhbmduYW1lIjoiXHU1ZTk1XHU1N2ZhXHU1YzQyIiwiR2V0VGltZSI6IjIwMjAtMDYtMjQgMTA6MzA6MzYifQ==</tem:jsonObj>
      </tem:OperationMethod>
   </soapenv:Body>
</soapenv:Envelope>";

// 构造curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.****.com/UpdateOperation.asmx");
curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Content-length: " . strlen($data)));
$res = curl_exec($curl);

if (!curl_errno($curl)) {
    $info = curl_getinfo($curl);
    echo '使用 ' . $info['total_time'] . ' 秒执行到 ' . $info['url'] . ' 的请求';
} else {
    echo 'Curl 错误信息: ' . curl_error($curl);
}
curl_close($curl);

// 获取传输信息
var_dump($info);

echo '执行结果:======================' . '<br>';
var_dump($res);

    以上代码,执行成功,结果如下:

使用 4299.709296 秒执行到 https://api.****.com/UpdateOperation.asmx 的请求

E:\***\curl_soap.php:34:array (size=26)
  'url' => string 'https://api.****.com/UpdateOperation.asmx' (length=43)
  'content_type' => string 'text/xml; charset=utf-8' (length=23)
  'http_code' => int 200
  'header_size' => int 274
  'request_size' => int 157
  'filetime' => int -1
  'ssl_verify_result' => int 20
  'redirect_count' => int 0
  'total_time' => float 4299.709296
  'namelookup_time' => float 4299.475296
  'connect_time' => float 4299.491296
  'pretransfer_time' => float 4299.616296
  'size_upload' => float 1558
  'size_download' => float 512
  'speed_download' => float 0
  'speed_upload' => float 0
  'download_content_length' => float 512
  'upload_content_length' => float 1558
  'starttransfer_time' => float 4299.647296
  'redirect_time' => float 0
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '***.***.***.**' (length=14)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 443
  'local_ip' => string '192.168.8.123' (length=13)
  'local_port' => int 45945
  
执行结果:======================
E:\***\curl_soap.php:36:string 
'<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" 
    <soap:Body>
        <OperationMethodResponse xmlns=" 
            <OperationMethodResult>[{"bflag":false,"ukey":"3a1382cb6db060b30e8e1f2a67e9deb0","msg":"fail"}]
            </OperationMethodResult>
        </OperationMethodResponse>
    </soap:Body>
</soap:Envelope>' (length=512)


欢迎分享交流,转载请注明出处:WithCoder » PHP使用Curl调用https协议的WebService