【问题标题】:Execute SOAP using cURL使用 cURL 执行 SOAP
【发布时间】:2012-01-26 11:50:02
【问题描述】:

我正在尝试使用 cURL 执行 SOAP 函数(因为使用 SoapClient() 时出现错误。

这是我的代码(正在运行中)

$credentials = "username:pass"; 
$url = "https://url/folder/sample.wsdl"; 
$page = "/folder"; 
$headers = array( 
    "POST ".$page." HTTP/1.0", 
    "Content-type: text/xml;charset=\"utf-8\"", 
    "Accept: text/xml", 
    "Cache-Control: no-cache", 
    "Pragma: no-cache", 
    "SOAPAction: \"customerSearch\"", 
    "Authorization: Basic " . base64_encode($credentials) 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']); 
$data = curl_exec($ch); 

问题是没有执行 SOAP 操作。而且我还需要将参数传递给操作。这甚至可能吗?

【问题讨论】:

    标签: php soap curl


    【解决方案1】:

    您需要将 cURL 选项指定为 POST,并设置请求的正文 - 如果您不发送正文,则 POST 请求没有意义(更重要的是,它不是 SOAP)。构建一个完整的 HTTP 请求标头并不能解决问题。

    $credentials = "username:pass"; 
    $url = "https://url/folder/sample.wsdl";
    $body = ''; /// Your SOAP XML needs to be in this variable
    
    $headers = array( 
        'Content-Type: text/xml; charset="utf-8"', 
        'Content-Length: '.strlen($body), 
        'Accept: text/xml', 
        'Cache-Control: no-cache', 
        'Pragma: no-cache', 
        'SOAPAction: "customerSearch"'
    ); 
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
    
    // Stuff I have added
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $credentials);
    
    $data = curl_exec($ch); 
    

    【讨论】:

      猜你喜欢
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 2016-06-18
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多