Welcome to WebmasterWorld Guest from 54.234.38.8

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

cURL / POST Connection issue

cURL / POST Connection issue

     
12:14 pm on Dec 9, 2011 (gmt 0)

New User

joined:Aug 15, 2011
posts: 26
votes: 0


Hello, I am trying to send a http POST request to a remote server with a specific port and path with XML data. Communication to the server is available at:

http://example.com:99999/pathname

I've tried doing this with both cURL and fsockopen. I've previosuly done this on a different system with cURL without any problems but I've not had to deal wth the combination of port number and path before and I think this might be causing me the problem.

Below is one of many cURL tries I've done, it returns: Curl error: No URL set!

error_reporting(E_ALL);
ini_set('display_errors', 'On');
$request = '<?xml version="1.0" encoding="UTF-8" ?>XML IN HERE';
$url = "http://example.com:99999/pathname";
$ch = curl_init();
curl_setopt($ch, curlOPT_URL, $url);
curl_setopt($ch, curlOPT_POST, 1);
curl_setopt($ch, curlOPT_POSTFIELDS, urlencode($request));
curl_setopt($ch, curlOPT_HEADER, 1);
curl_setopt($ch, curlOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, curlOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
echo '<br />Curl error: ' . curl_error($ch);
curl_close($ch);


I've also tried separating the port off into the curl_setopt($ch, curlOPT_PORT, $port) with $port set but this returns the same error.

Can anyone point me in the right direction? Any help appreciated.
3:09 pm on Dec 9, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


curl_setopt($ch, curlOPT_URL, $url);


The constants should be all caps... eg. CURLOPT_URL

I would expect curlOPT_URL to be undefined and this should throw an E_NOTICE depending on your error_reporting level?

I was also going to suggest try setting the port explicitly (as you also mention):
curl_setopt($ch, CURLOPT_PORT, 99999); // Integer
5:36 pm on Dec 9, 2011 (gmt 0)

New User

joined:Aug 15, 2011
posts: 26
votes: 0


Thanks, I've changed all the curlOPTs to CURLOPTs now but that doesn't change it.

I've also tried setting the port explicity again like you suggested. When doing this should the URL be amended to remove the port or would it stay as http://example.com:99999/pathname for example?

Thanks.
5:39 pm on Dec 9, 2011 (gmt 0)

New User

joined:Aug 15, 2011
posts: 26
votes: 0


Just an update on the error message I get. Having capitalized all my CURLOPTS the error coming back has now changed, it is no longer "Curl error: No URL set!" but is now:

Curl error: couldn't connect to host
5:45 pm on Dec 9, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


>>When doing this should the URL be amended to remove the port

The URL should be protocol-specific, and since we are using HTTP I believe it should work as is. But you might as well try it without the port in there to see if you get different results.

Also, are you in control of the server on the other end? Is it up and running before you run around looking for problems on your side? Perhaps a firewall in the way?
5:52 pm on Dec 9, 2011 (gmt 0)

New User

joined:Aug 15, 2011
posts: 26
votes: 0


Thanks, I've tried as you suggested with the URL just to check (http://example.com/pathname) and with the port specified, all below:

error_reporting(E_ALL);
ini_set('display_errors', 'On');
$request = '<?xml version="1.0" encoding="UTF-8" ?>XML IN HERE';
$url = "http://example.com/pathname";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 99999);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($request));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
echo '<br />Curl error: ' . curl_error($ch);
curl_close($ch);

That doesn't make any difference. I'm not in control of the server on the other end. But I've been told it's available and am currently trying to double check this with them.
6:00 pm on Dec 9, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Can you telnet to it?
6:29 pm on Dec 9, 2011 (gmt 0)

New User

joined:Aug 15, 2011
posts: 26
votes: 0


I've just tried this from my webserver and it reported back "Connection Timed Out".
11:34 am on Dec 10, 2011 (gmt 0)

New User

joined:Aug 15, 2011
posts: 26
votes: 0


Just as a follow-up I've tried sending a request using hurl.it and it works. This suggests the server is responding OK, so is it something to do with my code above that is wrong or my webserver maybe? Any help appreciated, thanks.
9:53 am on Dec 11, 2011 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


Just a couple things I noted here. The xml content you are trying to send, is there a parameter name via which you should be sending it? You have not shown one and without a parameter name the server on the other end will not know where your xml is within POST. So,
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($request));

Should most likely be:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($request));

Change 'xml' in the 'xml=' part to the appropriate parameter name which the server on the other end will be expecting to find the xml within.

Then one other thing, add this in to your code:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));


Not saying any of that will help with port addressing issues, just things I observed as missing and most likely needed anyways.
12:51 pm on Dec 11, 2011 (gmt 0)

New User

joined:Aug 15, 2011
posts: 26
votes: 0


Hi thanks.

I've added the extra CURLOPT_HTTPHEADER line and this didn't make any difference. Same error: "Curl error: couldn't connect to host"

I don't know of a parameter name for the XML - it was something I had thought about as well and will enquire about. Although whether this is the issue at the moment with the error being related to connecting to the host, I'm not sure.
4:06 pm on Dec 13, 2011 (gmt 0)

New User

joined:Aug 15, 2011
posts: 26
votes: 0


As an update to this, having contacted my hosting company it turns out they were preventing communication on this port. Now they've opened it all I can at least connect and we're making progress!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members