PHP cURL sending to port 8080

41,256

Solution 1

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.

Solution 2

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.

Share:
41,256
VeeBee
Author by

VeeBee

Updated on September 14, 2020

Comments

  • VeeBee
    VeeBee over 3 years

    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?

  • dkasipovic
    dkasipovic about 10 years
    How exactly is 990 standard?
  • SilentSteel
    SilentSteel about 10 years
    Corrected it to 443. Sorry was working with FTPS that day and wrote the wrong number.
  • Nilpo
    Nilpo about 10 years
    Yes, this works, but it's not necessarily any "simpler". It saves you one line, but at the cost of readability and extensibility.