How can I send SOAP XML via Curl and PHP?

82,158

Solution 1

Thanx a lot buddy, your code has worked for me.

Here is the code:

$soap_do = curl_init(); 
curl_setopt($soap_do, CURLOPT_URL,            $url );   
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); 
curl_setopt($soap_do, CURLOPT_TIMEOUT,        10); 
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($soap_do, CURLOPT_POST,           true ); 
curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $post_string); 
curl_setopt($soap_do, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($post_string) )); 
curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);

$result = curl_exec($soap_do);
$err = curl_error($soap_do);  

Solution 2

I had to use

$headers = array(             
    "Content-type: text/xml;charset=\"utf-8\"", 
    "Accept: text/xml", 
    "Cache-Control: no-cache", 
    "Pragma: no-cache", 
    "SOAPAction: \"run\"", 
    "Content-length: ".strlen($xml),
); 

and

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

Solution 3

For those finding this from Google, I ran into a similar problem, trying to interact with a .NET SOAP server from PHP, when the ASP method worked fine.

I used a packet sniffer to see what the ASP client was sending, exactly, and noticed it included cookies after the initial authentication request. So I enabled cookies in my cURL and it worked fine.

$cookiePath = tempnam('/tmp', 'cookie');
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);

Solution 4

This may be old, but this work for me. I originally had this function for an XML post and soap would not work until I change the HTTPHEADER to text/xml instead of application/xml:

function doXMLCurl($url,$postXML){
    $CURL = curl_init();

    curl_setopt($CURL, CURLOPT_URL, $url); 
    curl_setopt($CURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($CURL, CURLOPT_POST, 1); 
    curl_setopt($CURL, CURLOPT_POSTFIELDS, $postXML); 
    curl_setopt($CURL, CURLOPT_HEADER, false); 
    curl_setopt($CURL, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($CURL, CURLOPT_HTTPHEADER, array('Accept: text/xml','Content-Type: text/xml'));
    curl_setopt($CURL, CURLOPT_RETURNTRANSFER, true);
    $xmlResponse = curl_exec($CURL); 

    return $xmlResponse;
}

Solution 5

Try and set the port number using CURLOPT_PORT as perhaps it's not liking it as part of the URL?

Share:
82,158
dan richardson
Author by

dan richardson

Derbyshire freelance web developer

Updated on December 04, 2020

Comments

  • dan richardson
    dan richardson over 3 years

    This has been bugging me for days; I'm trying to send a SOAP post via Curl but I just keep getting a "couldn't connect to host" error, but, I really can't see how.

    I have an ASP version which works fine with the same URL and data. I think it's just a PHP/Curl thing.

    I currently have the following code (the CURLOPT_POSTFIELDS data is a valid SOAP envelope string):

    $soap_do = curl_init();
    curl_setopt($soap_do, CURLOPT_URL,            "https://xxx.yyy.com:517/zzz.asmx" );
    curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($soap_do, CURLOPT_TIMEOUT,        10);
    curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($soap_do, CURLOPT_POST,           true );            
    curl_setopt($soap_do, CURLOPT_POSTFIELDS,     '<soap:Envelope>...</soap:Envelope>'); 
    curl_setopt($soap_do, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen('<soap:Envelope>...</soap:Envelope>') ));
    
    if(curl_exec($soap_do) === false)
    {                
        $err = 'Curl error: ' . curl_error($soap_do);
        curl_close($soap_do);
        return $err;
    }
    else
    {
        curl_close($soap_do); 
        return 'Operation completed without any errors';
    }
    

    So any ideas why it just errors all the time?

    The ASP version works fine! The code is:

    Set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
    xmlhttp.Open "POST","https://xxx.yyy.com:517/zzz.asmx"
    xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    xmlhttp.Send('<soap:Envelope>...</soap:Envelope>')
    
  • dan richardson
    dan richardson almost 14 years
    Already tried that one and it doesn't work :/ Thanks though for the idea
  • dan richardson
    dan richardson almost 14 years
    Thanks Dom, I will give that a shot later on and let you know if it worked for me :)
  • Josh
    Josh over 10 years
    After 2 days of working out why it wasnt working, this fixed it!
  • dan richardson
    dan richardson over 10 years
    I never got an answer which worked for me, and this was over 2 years ago now. People seem to find this answer useful, so I will mark it as the answer. Thanks.
  • JasonWoof
    JasonWoof over 10 years
    +1 for SOAPAction: "run". That was the missing piece required by my soap server.
  • mahen3d
    mahen3d over 7 years
    Can someone help me out with vice versa of this ie turn curl request like above to a soapclient request?