0

I am trying to get information of remote file with curl. Problem is that other web-server is on port 81.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt ($ch, CURLOPT_PORT , 81);
curl_setopt($ch, CURLOPT_URL, 'http://98.246.25.185/server_status2.php');
$store = curl_exec ($ch);
echo substr($store, 1);
curl_close ($ch);
?>

And as you can see it doesnt work.

3
  • <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt ($ch, CURLOPT_PORT , 81); curl_setopt($ch, CURLOPT_URL, '98.246.25.185/server_status2.php'); $store = curl_exec ($ch); echo substr($store, 1); curl_close ($ch); ?> Commented Dec 19, 2009 at 17:11
  • 1
    @DanSpd : the code is not messaed up by the "stupid script" : you just didn't respect the code-formating specification : on the right of the edit form, there should be a quick reference about the syntax, and a link to the full reference : stackoverflow.com/editing-help ;; and in the toolbar of the edit-form, there is an icon with some 0 and 1, which indents code so it is formated. Commented Dec 19, 2009 at 17:14
  • Maybe my browser messes it up when I use it? Commented Dec 19, 2009 at 17:18

3 Answers 3

5

What happens if you specify the port number in the URI ?

What I mean is first remove the CURLOPT_PORT line, and, then, modify the CURLOPT_URL one to add the port number :

curl_setopt($ch, CURLOPT_URL, 'http://98.246.25.185:81/server_status2.php');


Edit after the comment : I just tryied this portion of code :

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt ($ch, CURLOPT_PORT , 81);
curl_setopt($ch, CURLOPT_URL, 'http://98.246.25.185:81/server_status2.php');
$store = curl_exec ($ch);
echo substr($store, 1);
curl_close ($ch);

And I get this output :

Online Peak: 59
Online: 17
Distributive server: Online
Agent server: Online

So, the code seems to be OK.

Are you sure there is not a firewall or anything or your network, that prevents you from doing HTTP requests on port 81 to that server ?
Does it work when you type that URI in your browser ?

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

2 Comments

it will keep loading for ever
I confirm it remains "waiting" forever ^^ Which is generally caused by some network connection not being possible : like if there were some firewall on your network, preventing your first server from connecting to that second server (I've seen that kind of situation of few times, where non-standard port where blocked by hosting providers -- maybe it's the case here ? )
2

Not sure if you're using SELinux or not (e.g. under Centos) but you may need to allow this specific permission (non-typical ports):

setsebool -P httpd_can_network_connect on

I would also strongly suggest adding additional configuration options to get any error output from CURL such as:

curl_setopt($ch,CURLOPT_FAILONERROR,1);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if($curl_errno > 0)
    die("cURL Error ($curl_errno): $curl_error");

Comments

1

Your original code works fine for me. Returns:

<b>Online Peak: </b>59        <br /><b>Online: </b> 17<br /><b>Distributive server:</b> <font color=green>Online</font><br /><b>Agent server:</b> <font color=green>Online</font><br />

Perhaps the port is being blocked by your firewall?

P.S. The echo in your code is redundant. The curl_exec prints the result to the output buffer unless you set CURLOPT_RETURNTRANSFER as TRUE.

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.