Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2002
    Location
    Perth, Western Australia
    Posts
    227

    [PHP] fsockopen() & Connection refused (111)

    I'm having problems using fsockopen() to connect to a local web server. I have set up port forwarding and everything already. If I manually type the address in my web browser it works. But through php's fsockopen() it does not connect.

    Heres a sample code:

    PHP Code:
    $fp fsockopen("xxx.xxx.xxx.xxx"8888$errno$errstr30);
    if (!
    $fp) {
       echo 
    "$errstr ($errno)<br />\n";
    } else {
       
    $out "GET / HTTP/1.0\r\n";
       
    $out .= "Host: xxx.xxx.xxx.xxx\r\n";
       
    $out .= "Connection: Close\r\n\r\n";

       
    fwrite($fp$out);
       while (!
    feof($fp)) {
           echo 
    fgets($fp128);
       }
       
    fclose($fp);


    I get;

    Warning: fsockopen(): unable to connect to xxx.xxx.xxx.xxx:8888 in
    /home/blah/www/scripts/test.php on line 5
    Connection refused (111)

    But when I access it internally via browser manually; ie: http://192.168.0.1:8888/scripts/test.php it works fine?!

    I tried accessing it from an external computer [with external IP]; http://xxx.xxx.xxx.xxx:8888/scripts/test.php and it works fine as well.

    The only thing I can think of is fsockopen doesn't like accessing resource if its a port forwarded resource by the router? I tried two different routers with port forwarding. Any other solutions greatly appreciated.
    <<Please RTFM for signature setup>>

  2. #2
    Is the webhost being run off the same computer you are trying to access it with?
    192.*.*.* ip addresses are 'private' router ip's. They are not public.
    get your real ip from whatsmyip.org and try that in the script.

    And if your already using your public ip, attempt using the IP given by the router.

    Sorry if this is not helpful and I understood you incorrectly.
    Ray

  3. #3
    Join Date
    Jul 2002
    Location
    Perth, Western Australia
    Posts
    227
    Yeah its not an internal IP. I have it set up where I can access it via internal IP or external IP hence the port forwarding.
    <<Please RTFM for signature setup>>

  4. #4
    Join Date
    Sep 2005
    Location
    Southern California
    Posts
    179
    Is the server running this malfunctioning PHP script located on your network?

    If so, with most routers you will have to use the internal IP instead of the external for any machines on the same network.

  5. #5
    Join Date
    Jul 2002
    Location
    Perth, Western Australia
    Posts
    227
    Quote Originally Posted by localhost127
    Is the server running this malfunctioning PHP script located on your network?

    If so, with most routers you will have to use the internal IP instead of the external for any machines on the same network.
    This is how its currently set up:

    WEBHOST -> INTERNET -> ADSL Router [Port Forward 8888 Apache] -> Local Lan -> Local Workstation [Apache port 8888]

    WEBHOST contains a script where the visitor signs up for a account. This script checks whether the visitors phone number exist or not. The database it queries to check this is on a local server which is a MYOB Premier with ODBC+PHP [check.php?phone=blah]. This returns a result whether it exists or not.

    I can access this script manually with http://real-ip:8888/check.php?phone=blah and it works fine. But using that in the PHP script with fsockopen() does not work and I get a Warning: fsockopen(): unable to connect to xxx.xxx.xxx.xxx:8888 in
    /home/blah/www/scripts/check.php on line 5 Connection refused (111) error.

    Very weirrrrrrrdddddddddd :O
    <<Please RTFM for signature setup>>

  6. #6
    Join Date
    Sep 2005
    Location
    Southern California
    Posts
    179
    It is possible your web host is firewalling your external connections. Perhaps try connecting to google on port 80 or something just to verify that is not the issue.

  7. #7
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    It might be that your webhost has restricted ports being opened by the Apache process, a lot do this to prevent spamming.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  8. #8
    Join Date
    Jul 2002
    Location
    Perth, Western Australia
    Posts
    227
    If I specify another website like Google and others, it will work. I'll try port triggering instead of port forwarding in my adsl router when I return to work tomorrow.
    <<Please RTFM for signature setup>>

  9. #9
    Join Date
    Jul 2002
    Location
    Perth, Western Australia
    Posts
    227
    It ended up to be my ISP which blocked the common ports 80/443. I had to toggle it off in the control panel. The error still occured if I used a different port. So I guess Apache will only work with 80 which is the way the ISP configured PHP/Apache.
    <<Please RTFM for signature setup>>

  10. #10
    Join Date
    Mar 2006
    Posts
    418
    Just a thought, but you sure that you should be sending Host: in the header? I don't remember 100% correct, but i think i had the same problem, working off ip's and sending host in the header...
    || Semi-professional PHP developer || Exams right now, don't I just feel lucky? ||

  11. #11
    @onture
    How you managed to solve this issue then ?
    Please share here. i am also facing the same issue while using ozeki sms server

  12. #12
    Join Date
    Nov 2005
    Posts
    268
    Try using curl instead of fsock to open the web page and see if that makes any difference.

  13. #13
    Join Date
    Jun 2010
    Posts
    71
    i maybe way off base, but if your trying to redirect;

    header("Location: http://domain.com/blah/di/blah.php");

    or the thing i tend to use to run remote scripts is:

    $webby = "http://domain.com/blah/blah/blah";
    $result = file_get_contents($webby);

    $xyz = $result;

    if $xyz.... etc

  14. #14
    Join Date
    May 2009
    Posts
    766
    What happens if you try:

    Code:
    telnet xxx.xxx.xxx.xxx 8888
    from the same machine that's executing the php script? Let's try to isolate whether this is a networking issue, or an issue with fsockopen()...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •