Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cURL sending to port 8080

Tags:

php

curl

port

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?

like image 289
VeeBee Avatar asked Jun 09 '11 08:06

VeeBee


2 Answers

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.

like image 68
SilentSteel Avatar answered Oct 22 '22 13:10

SilentSteel


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.

like image 1
Ohadsh Avatar answered Oct 22 '22 12:10

Ohadsh