1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

fopen file_get_contents curl not able fetch URL content of same IP or domain

Discussion in 'PHP' started by manwanis, Feb 15, 2009.

  1. #1
    Hi

    My code to fetch URL content of same IP or same domain when using fopen

    $fp = @fopen("http://www.example.com","r");
    while(!feof($fp))
    {
    $cont.= fread($fp,1024);
    }
    fclose($fp);

    However when I try other domain then code works fine.

    I also tried file_get_contents and curl and found same issue.
    Then I thought that it could be a problem with my server but I found if above code is written on other domain then my application URL return HTML code without an issue.

    Please suggest.
     
    manwanis, Feb 15, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    what DOES it return then? It may be a DNS / server config issue. you should check your apache logs for the request and see why it does not serve the right virtual host. not that it's any of my business but modern best practices / PHP configuration directives advise to NOT allow file opening over remote protocols, from default php.ini:
    ;;;;;;;;;;;;;;;;;;
    ; Fopen wrappers ;
    ;;;;;;;;;;;;;;;;;;
    
    ; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
    allow_url_fopen = Off
    
    PHP:
    Most exploits rely on this being enabled in order to callback and fetch some files or simply include remote php. (include/require also depend on this setting).

    Curl is considered safer.
     
    dimitar christoff, Feb 15, 2009 IP
  3. manwanis

    manwanis Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Currently I get a chain of messages:

    [15-Feb-2009 12:14:05] PHP Warning: feof(): supplied argument is not a valid stream resource in /home/mydomain/public_html/testfolder/test.php on line 56

    [15-Feb-2009 12:14:05] PHP Warning: fread(): supplied argument is not a valid stream resource in /home/mydomain/public_html/testfolder/test.php on line 58

    I also checked my php.ini and found that
    allow_url_fopen = on

    Any other aspect I should look into.
     
    manwanis, Feb 15, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    well - then i do believe it is a dns issue - can it resolve correctly? try echo gethostbyname("www.yourhost.com"); and see if it gets back with the right ip. does your box have more than 1 ip address bound to the same interface?

    you can also try a socket connection for more info like:
    $fo = fsockopen("www.yourhost.com", 80, $errno, $errstr, 12); // timeout in sec
    //and check the $errno and $errstr...
    echo "Error $errno: $errstr<br />";
    
    fputs($fo, "GET /filename.php HTTP/1.0\r\n");
    fputs($fo, "Host: www.yourdomain.com\r\n");
    fputs($fo, "Referer: http://www.digitalpoint.com\r\n");
    fputs($fo, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n\r\n");
    echo "Error $errno: $errstr<br />";
    
    // treat $fo like a normal file from hereon end.
    
    PHP:
    and finally - ssh into your box and try from the command line:

    lynx http://www.yoursite.com/
    or
    wget http://www.yoursite.com/

    ... and read the file it fetches. the idea is, if command line does not get the right virtual host, PHP certainly won't. you may want to edit your hosts file and bind the correct ip to your host somewhere there. good luck
     
    dimitar christoff, Feb 16, 2009 IP
  5. manwanis

    manwanis Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here is what I got when I ran below script (I have replaced my domain name in below message):

    Warning: fsockopen() [function.fsockopen]: unable to connect to www.mydomain.com:80 (Connection timed out) in /home/mydomain/public_html/testfolder/test.php on line 3
    Error 110: Connection timed out


    Warning: fputs(): supplied argument is not a valid stream resource in /home/mydomain/public_html/testfolder/test.php on line 7

    Warning: fputs(): supplied argument is not a valid stream resource in /home/mydomain/public_html/testfolder/test.php line 8

    Warning: fputs(): supplied argument is not a valid stream resource in /home/mydomain/public_html/testfolder/test.php on line 9

    Warning: fputs(): supplied argument is not a valid stream resource in /home/mydomain/public_html/testfolder/test.php on line 10
    Error 110: Connection timed out

    The box is of my hosting company. the above code works fine when I replace my domain with google

    Here is what I get when pointed to google:
    Error 0:
    Error 0:

    I do not have ssh access to my server.
     
    manwanis, Feb 16, 2009 IP
  6. manwanis

    manwanis Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    any suggestion ???
     
    manwanis, Feb 17, 2009 IP
  7. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You will get that error if the file doesn't exist or it doesn't have permission to access the file.

    Try changing your file permissions of that page to 755 or 777 and see if you get the same error.
     
    websecrets, Feb 17, 2009 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8
    you won't be able to fix this - basically, there is a conflict of ip addresses. your php is just fine. as its shared hosting and you don't get ssh access and dont get access to error logs... hard to tell. i'd contact support and ask them to perform the ssh test for you in addition to checking what yourdomain.com resolves to locally and trying to match against external dns entry.
     
    dimitar christoff, Feb 17, 2009 IP
  9. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Also check to see if you have safe_mode enabled/disabled.
     
    websecrets, Feb 17, 2009 IP
  10. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #10
    nah - he already said it works on other domains.
     
    dimitar christoff, Feb 17, 2009 IP
  11. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yeah, but that doesn't mean that safe_mode can't be blocking read access to his own files.

    What is your actual URL? I'll see if I can open it from here.
     
    websecrets, Feb 17, 2009 IP
  12. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #12
    but it can't know they are his own files, he's calling it by domain. in theory he can have 100s of domains all pointing to the same ftp space - php compiler should remain independent of the apache vhosts so i doubt this is the case.
     
    dimitar christoff, Feb 17, 2009 IP
  13. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    See if this code works... try showing the content then try writing the content to a file. If you can show it but not write it then it's a permission/ownership issue. If you get an error, post it.

    function get_content($url)
    {
    $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_HEADER, 0);

    ob_start();

    curl_exec ($ch);
    curl_close ($ch);
    $string = ob_get_contents();

    ob_end_clean();

    return $string;
    }

    $content = get_content ("http://yoursite");
     
    websecrets, Feb 17, 2009 IP