Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send SOAP XML via Curl and PHP?

Tags:

php

soap

curl

xml

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>') 
like image 214
dan richardson Avatar asked Jun 09 '10 14:06

dan richardson


People also ask

Can I use cURL in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.

How does PHP handle cURL request?

php file with the following contents. $url = 'https://www.example.com' ; $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url );


1 Answers

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);   
like image 95
Syn Avatar answered Sep 19 '22 19:09

Syn