Skip to main content

Send SOAP XML via curl, PHP






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 cant 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>')




Source: Tips4all

Comments

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

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

    ReplyDelete
  3. 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);

    ReplyDelete
  4. 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);

    ReplyDelete
  5. Your also using the wrong Content-type, application/xml is the correct type. But that shouldn't make any difference to the cURL request.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...