使用cURL执行SOAP



我正试图使用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操作。我还需要为行动传递论据。这可能吗?

您需要指定POST的cURL选项,并设置请求的正文-如果您不发送正文,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); 

最新更新