简体   繁体   中英

Execute SOAP using cURL

I am trying to execute an SOAP-function using cURL (because I get an error using the SoapClient().

This is my code (that is halfway working)

$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); 

The problem is that the SOAP-action is not being performed. And I also need to pass arguments to the action. Is this even possible?

You need to specify the cURL options to POST, and set the body of the request - if your not sending a body, there's no point in a POST request (and more importantly, it isn't SOAP). Building a complete HTTP request header just won't cut it.

$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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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