homepage Welcome to WebmasterWorld Guest from 54.205.59.78
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
cURL / POST Connection issue
cURL / POST Connection issue
Piggo




msg:4395961
 12:14 pm on Dec 9, 2011 (gmt 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.

 

penders




msg:4396004
 3:09 pm on Dec 9, 2011 (gmt 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
Piggo




msg:4396077
 5:36 pm on Dec 9, 2011 (gmt 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.

Piggo




msg:4396085
 5:39 pm on Dec 9, 2011 (gmt 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

eelixduppy




msg:4396086
 5:45 pm on Dec 9, 2011 (gmt 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?

Piggo




msg:4396087
 5:52 pm on Dec 9, 2011 (gmt 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.

eelixduppy




msg:4396088
 6:00 pm on Dec 9, 2011 (gmt 0)

Can you telnet to it?

Piggo




msg:4396104
 6:29 pm on Dec 9, 2011 (gmt 0)

I've just tried this from my webserver and it reported back "Connection Timed Out".

Piggo




msg:4396318
 11:34 am on Dec 10, 2011 (gmt 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.

astupidname




msg:4396592
 9:53 am on Dec 11, 2011 (gmt 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.

Piggo




msg:4396612
 12:51 pm on Dec 11, 2011 (gmt 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.

Piggo




msg:4397460
 4:06 pm on Dec 13, 2011 (gmt 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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved