PHP : Curl https xml result returns blank

标签 php web-services ssl curl soap

需要您的帮助!我正在尝试使用 curl 创建一个 PHP soap 客户端。当我运行我的 PHP 代码时。我得到空白结果。 这是一个 https 连接。我通过这个 example 使用 OpenSSL 生成我的 .pem 文件

PHP 代码

    $xml = '<?xml version="1.0" encoding="utf-8"?>'.
        '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://olp.sadad.com/sadadpaymentmanagement/service/olppaymentmanager/req">'.
           '<x:Header/>'.
            '<x:Body>'.
        '<req:initiatePaymentDetailsReq>'.
           '<req:olpIdAlias>*****</req:olpIdAlias>'.
             '  <req:merchantId>*****</req:merchantId>'.
        '<req:merchantRefNum>999454564</req:merchantRefNum>'.
            '<req:paymentAmount>200</req:paymentAmount>'.
            '<req:paymentCurrency>SAR</req:paymentCurrency>'.
            '<req:dynamicMerchantLandingURL></req:dynamicMerchantLandingURL>'.
            ' <req:dynamicMerchantFailureURL></req:dynamicMerchantFailureURL>'.
              '</req:initiatePaymentDetailsReq>'.
              '</x:Body>'.
              '</x:Envelope>';
    try{
    $url = "https://b2brbtest.riyadbank.com/soap?service=RB_OLP_INITIATE_PAYMENT";
     $certFile =  dirname(__FILE__)."/key1.pem";
      $certPass = "*******";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSLCERT, $certFile);
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $certPass);
    $headers = array();
    array_push($headers, "Content-Type: text/xml; charset=utf-8");
    array_push($headers, "SOAPAction: sii:RB_OLP_INITIATE_PAYMENT");

    if($xml != null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
        array_push($headers, "Content-Length: " . strlen($xml));
    }

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    var_dump($response);

    }  catch (Exception $e){
    echo $e->getMessage();


    }

    ?>

输出

string(228) " "

最佳答案

最后我通过更改 $xml 值解决了这个问题

$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://olp.bank.com/sappay/service/olppaymentmanager/req">
   <soapenv:Header/>
   <soapenv:Body>
      <req:initiatePaymentDetailsReq>
         <olpIdAlias>****</olpIdAlias>
         <merchantId>***</merchantId>
         <merchantRefNum>'.rand(1, 15).rand().'</merchantRefNum>
         <paymentAmount>'.$tb.'</paymentAmount>
         <paymentCurrency>SAR</paymentCurrency>
         <dynamicMerchantLandingURL></dynamicMerchantLandingURL>
         <dynamicMerchantFailureURL></dynamicMerchantFailureURL>
      </req:initiatePaymentDetailsReq>
   </soapenv:Body>
</soapenv:Envelope>';

这里是如何将 SOAP 网络服务与 ssl 连接的完整代码

<?php
$tb='200';

$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://olp.bank.com/sappay/service/olppaymentmanager/req">
   <soapenv:Header/>
   <soapenv:Body>
      <req:initiatePaymentDetailsReq>
         <olpIdAlias>*****</olpIdAlias>
         <merchantId>********</merchantId>
         <merchantRefNum>'.rand(1, 15).rand().'</merchantRefNum>
         <paymentAmount>'.$tb.'</paymentAmount>
         <paymentCurrency>SAR</paymentCurrency>
         <dynamicMerchantLandingURL></dynamicMerchantLandingURL>
         <dynamicMerchantFailureURL></dynamicMerchantFailureURL>
      </req:initiatePaymentDetailsReq>
   </soapenv:Body>
</soapenv:Envelope>';
try{
$url = "https://b2b.rb.com/soap?service=RB_OLP_INITIATE_PAYMENT";
 $certFile =  dirname(__FILE__)."/key1.pem";
  $certPass = "*****";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_SSLCERT, $certFile);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $certPass);
$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "SOAPAction: sii:RB_OLP_INITIATE_PAYMENT");
array_push($headers, "Host:b22.rb.com");

if($xml != null) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    array_push($headers, "Content-Length: " . strlen($xml));
}

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
     print_r($response);
}  catch (Exception $e){
echo $e->getMessage();
   }


?>

关于PHP : Curl https xml result returns blank,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39571363/

相关文章:

php - 电子邮件地址验证方法(订阅按钮)

php - 如何使用此代码获取user_id

c# - 即使正确发送了 SOAP 请求,Axis2 也总是收到空参数?

web-services - 了解 CORS

javascript - 解析兼容性和安全性

php - 如何避免TCPDF错误抑制系统?

asp.net - 为什么我们需要在 Web 服务中序列化

ruby-on-rails - 我怎么知道我的数据是否真的在 Heroku 上使用 SSL 传输?

openssl - libcurl 与 OpenSSLUnknown SSL 协议(protocol)连接错误 -(无服务器问候) - 为什么?

php - 抓取 PHP 文件中特定标签之间的文本