14

today I am trying to make a curl call to somesite which is listening to port 8080. However, calls like this get sent to port 80 and not 8080 instead:

$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);

am i missing something here?

8
  • You could try somesite.tld:8080 as url. Commented Jun 9, 2011 at 8:12
  • i've asked them already and told me it was open. btw, will curl requests default to port 80 if the specified port is locked? Commented Jun 9, 2011 at 8:17
  • 3
    And what exactly makes you think they're directed at port 80 instead of port 8080? Are you absolutely certain that the var you specify in CURLOPT_PORT is 8080? Because it should work, either setting the port with CURLOPT_PORT and using the URL without port number in CURLOPT_URL, or by specifying the complete URL (including port) in CURLOPT_URL, without using CURLOPT_PORT. Commented Jun 9, 2011 at 8:33
  • 1
    @wimvds: because the remote server also listens to port 80 but outputs a different response. Say port 80 replies 1 and port 8080 replies 0. The reply I am expecting is supposed to be 0 but instead i get a 1. @kamil & arvin: i will try. Commented Jun 9, 2011 at 8:39
  • 1
    For debugging such issues you could add curl_setopt($ch, CURLOPT_VERBOSE, 1) and see the debugg info in web server error log Commented Sep 15, 2013 at 16:13

8 Answers 8

4

Just a note, I've had this issue before because the web host I was using was blocking outbound ports aside from the standard 80 and 443 (HTTPS). So you might want to ask them or do general tests. In fact, some hosts often even block outbound on port 80 for security.

Sign up to request clarification or add additional context in comments.

2 Comments

How exactly is 990 standard?
Corrected it to 443. Sorry was working with FTPS that day and wrote the wrong number.
1

Simple CURL GET request: (Also added json/headers if required, to make your life easier in need)

<?php
$chTest = curl_init();
curl_setopt($chTest, CURLOPT_URL, "http://example.com:8080/?query=Hello+World");
curl_setopt($chTest, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($chTest, CURLOPT_HEADER, true);
curl_setopt($chTest, CURLOPT_RETURNTRANSFER, true);

$curl_res_test = curl_exec($chTest);
$json_res_test = explode("\r\n", $curl_res_test);

$codeTest = curl_getinfo($chTest, CURLINFO_HTTP_CODE);
curl_close($chTest);
?>

Example POST request:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com:8080');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"Hello" : "World", "Foo": "World"}');
// Set timeout to close connection. Just for not waiting long.
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$curl_res = curl_exec($ch);
?>

Charset is not a required HTTP header and so are the others, the important one is Content-Type.

For the examples I used JSON MIME type, You may use whatever you want, take a look at the link: http://en.wikipedia.org/wiki/Internet_media_type

Make sure that the php_curl.dll extension is enabled on your php, and also that the ports are open on the target serve.

Hope this helps.

Comments

0

First check that you're able to connect using something other than PHP's horrible Curl syntax:

else look at using (for linux users)

  • curl somesite:8080
  • wget -qO- somesite:8080

Once you've established you can connect, then you can go about the horrible business of using PHP Curl. There are wrappers, but they're flaky that I've found.

Both Curl, Wget or similar can be very easily configured to use GET and POST methods. It's not advisible, but for more than one complicated operation using Curl I've simply given up trying to configure PHP's library correctly and simply dropped to the command line.

THERE ARE SECURITY IMPLICATIONS. You need to take great care to ensure that anything you give it, particularly if it's from a form or an external source, is appropriately escaped.

<? 

//Strip out any possible non-alpha-numeric character for security

$stringToSend = preg_replace('[^a-zA-Z]', '', $stringToSend);

$return = shell_exec("curl -X POST -d $stringToSend http://example.com/path/to/resource");

Comments

0

Your web server is misconfigured. The code you provided works for me.

Also, your code can be simpler. Just put the URI into the init call and drop the CURLOPT_PORT and CURLOPT_URL lines:

$ch = curl_init('http://somesite.tld:8080');

1 Comment

Yes, this works, but it's not necessarily any "simpler". It saves you one line, but at the cost of readability and extensibility.
0

Maby you can use --local-port [-num]

Set a preferred number or range of local port numbers to use for the connection(s). Note that port numbers by nature are a scarce resource that will be busy at times so setting this range to something too narrow might cause unnecessary connection setup failures. (Added in 7.15.2)

Source: http://curl.haxx.se/docs/manpage.html#--ftp-pasv

Comments

0

Use PHP cURL to make a request to port 8080 on a server

$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);
       
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');

Comments

-1

try this option for curl

curl_setopt($ch, CURLOPT_PROXY,"localhost:8080");

or use this

curl_setopt($ch, CURLOPT_PROXY,"yourservername:8080");

Comments

-1
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "your-domain-name");
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_exec($ch); 
curl_close($ch); `
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.