SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member Patrick_M's Avatar
    Join Date
    Aug 2004
    Location
    Ohio
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is There A Way To Load Scripts Fast When Using This Code?

    Hi All,

    I have a couple of scripts that utilize a function I use for pulling
    a news page off of my website, but there is a bit of a problem
    with it.

    Here is the code I'm using now:
    Code:
    <?
    // Open news file
    function latestnews(){
          
          if($html = @fopen("http://www.domain.com/file.html", "r")){
            $html = file_get_contents("http://www.domain.com/file.html");
            return "$html";
          }else{
            $html = '<table align="center" width="100%" border="0"><tr><td align="center"><font face="verdana" size="2">Sorry, no news available at this time.</font></td></tr></table>';
            return "$html";
          }
    }
    ?>
    It works very well until my server is down, and then the script trying
    to load the "file.html" document takes forever to load.

    Is there anyway to set this to load everything in the script utilizing
    this function while waiting to pull the file from my server, rather than
    having to wait for the time out to happen, before everything else
    in the script loads?

    Hope that makes sense.

    Patrick

  2. #2
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're open 2 connections to the file, so try this:
    PHP Code:
    <?php
    // Open news file
    function latestnews(){
        
    // Opens the file, times out in 10 seconds so it doesn't take forever
        
    if ($open = @fsockopen("http://www.domain.com/file.html"80$en$es10)) {
            
    // While the file isn't at the end of the file
            
    while (!feof($open)) {
                
    // Add on to $html
                
    $html .= fgets($open);
                
    // Always test it
                
    if (!$open) {
                    
    $html '<table align="center" width="100%" border="0"><tr><td align="center"><font face="verdana" size="2">Sorry, no news available at this time.</font></td></tr></table>';
                    return 
    $html;
                }
            }

            return 
    $html;
        } else {
            
    $html '<table align="center" width="100%" border="0"><tr><td align="center"><font face="verdana" size="2">Sorry, no news available at this time.</font></td></tr></table>';
            return 
    $html;
        }
    }
    ?>
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  3. #3
    Non-Member Patrick_M's Avatar
    Join Date
    Aug 2004
    Location
    Ohio
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I gave that a try, but no matter what I set the timeout for, the
    file never loads and I get the "Sorry, no news available" message.

    Patrick

  4. #4
    Non-Member Patrick_M's Avatar
    Join Date
    Aug 2004
    Location
    Ohio
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I removed the @, and here are two Warning messages I am receiving:

    Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known

    Warning: fsockopen(): unable to connect to www.domain.com/news.html:80

    Patrick


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
  •