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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.