SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Time Limit Or Alternative File Opening

    I need a way to fopen a file from a remote server and read the data from the file into the script. The thing is, I want the script to either be able to return a custom error and continue loading if it can't open the file if the server is down. Currently, if I just fopen the file but the server is down, it takes forever to render the script or it doesn't finish rendering at all. I need a way to fix this. Is there any method?
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  2. #2
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps, this might work (copied from another forum):
    PHP Code:
    define(MAXTRIES3); 
    function 
    get_data($url) { 
       
    $trycount 0
       
    $succes FALSE
       while ((
    $trycount MAXTRIES) || (!$succes)) {    
          
    $filepointer fopen($url"r"); 
          if (
    $filepointer$succes TRUE
          
    $trycount++; 
       } 
       if (!
    $filepointer) die("Fatal error opening source location: $url "); 
       
       while (!
    feof($fp)) { 
          
    $fetchdata .= fgets($fp1024); 
       } 
       
    fclose($fetchdata); 
       return (
    $fetchdata); 

    My name is Steve, and I'm a super-villian.

  3. #3
    SitePoint Zealot rae's Avatar
    Join Date
    Apr 2003
    Location
    bedroom :P
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    Not sure, but try to get some info on socket functions in php.

    I think maybe the script should somehow firstly ping the server to be sure if the server isn't down. Then if it's ok, get the file. That's just my first thought!

  4. #4
    SitePoint Zealot rae's Avatar
    Join Date
    Apr 2003
    Location
    bedroom :P
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lieut_data:

    I've read what you post... /you were faster than me LOL /
    but I'm not sure about that script. It's using fopen, and i think it will normally try to open a remote file (url)...
    but...
    if the server isn't avaiable then it will hang until the connection times out. (is it in the php.ini?? as default I'm not sure :P)

    but if you use sockets /fsockopen()/, then you'll be able to set custom timeouts in milliseconds /socket_set_timeout()/ for each funcion calls. Then the script can run much more faster, because you don't need to wait for the default timeout period.

    I found that in the php doc:

    PHP Code:
    <?php
    $fp 
    fsockopen("www.example.com"80);
    if(!
    $fp) {
        echo 
    "Unable to open\n";
    } else {
        
    fputs($fp"GET / HTTP/1.0\n\n");
        
    $start time();
        
    stream_set_timeout($fp2);
        
    $res fread($fp2000);
        
    var_dump(stream_get_meta_data($fp));
        
    fclose($fp);
        print 
    $res;
    }
    ?>
    hope this helps!

  5. #5
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rae
    lieut_data:

    I've read what you post... /you were faster than me LOL /
    but I'm not sure about that script. It's using fopen, and i think it will normally try to open a remote file (url)...
    but...
    if the server isn't avaiable then it will hang until the connection times out. (is it in the php.ini?? as default I'm not sure :P)
    Yeah, I haven't tested it myself (just a copy and paste), (thanx!)

    Quote Originally Posted by rae
    but if you use sockets /fsockopen()/, then you'll be able to set custom timeouts in milliseconds /socket_set_timeout()/ for each funcion calls. Then the script can run much more faster, because you don't need to wait for the default timeout period.
    That was also one of the other examples, on http://forums.devshed.com/archive/5/2002/01/2/28341 (where I got my source from
    My name is Steve, and I'm a super-villian.

  6. #6
    SitePoint Member
    Join Date
    Nov 2005
    Location
    Belgium
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A bit late to answer but I just had the same problem using fopen or file_get_contents to read data from a distant file.

    I have tried to put a timeout on fsockopen but it is slow compared to the CURL library.

    So, I post my solution here in case someone has the same problem.

    PHP Code:
    function FileGetContentsCurl($url$getContent$timeout 10){
      
    $ch curl_init($url);
      
    curl_setopt($chCURLOPT_HEADER0);
      
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
      
    curl_setopt($chCURLOPT_TIMEOUT$timeout);
      
    $resultat curl_exec($ch);
      
    $CurlErr curl_error($ch);
      
    curl_close($ch);
      if (
    $CurlErr) {
        echo 
    $CurlErr;
        return 
    false;
      }elseif (
    $getContent){
        return 
    $resultat;
      }
    }

    $url        'http://localhost/test/sleep.php';
    $getContent true;                               
    $timeout    1;

    echo 
    FileGetContentsCurl($url$getContent $timeout); 

  7. #7
    SitePoint Addict
    Join Date
    Apr 2005
    Location
    San Diego, CA
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is difficult to solve if you don't know the status of the remote server. One solution might be to have a cronjob that creates a local copy of the file and your production script reads the local copy. Have the cronjob read the file as often as you need new data (ex. every 5 minutes). If you have multiple servers then you might assign the task to your least busy server and set it up so the file can be read by all your local computers. Hope the helps.
    I study speed waiting. I can wait an entire hour in 10 minutes.

  8. #8
    SitePoint Member
    Join Date
    Nov 2005
    Location
    Belgium
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPCamp.com
    That is difficult to solve if you don't know the status of the remote server.
    The CURL snippet above tests both the status of the server and the status of the requested file/page (200 OK status). Same can be achieved with fsockopen but it is not as fast. Every time I use CURL, I am impressed by its features.

    Quote Originally Posted by PHPCamp.com
    One solution might be to have a cronjob that creates a local copy of the file and your production script reads the local copy. Have the cronjob read the file as often as you need new data (ex. every 5 minutes).
    That's indeed the most practical solution but some application need to access data in real time. Some other scripts for example might test the dead links on a page. For that, fopen() can do the job but without timeout this may cause your application to freeze if the server is slow or fail to send the 200 OK status in a reasonable time. Further more, I believe that set_time_limit(), that changes the max_execution_time value, will not help as open streams are not accounted for in the execution time.

    In my quest for a solution I found that functions like stream_set_timeout() are working fine with sockets but not for streams opened by fopen() or file_get_contents().

    As of PHP 4.3, this function can (potentially) work on any kind of stream. In PHP 4.3, socket based streams are still the only kind supported in the PHP core, although streams from other extensions may support this function.
    http://www.php.net/manual/en/functio...et-timeout.php
    See also: http://bugs.php.net/bug.php?id=5153

    I really think that a proper timeout should be introduced for both fopen() and file_get_contents(). In the meantime I will use my little snippet above.


Bookmarks

Posting Permissions

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