+ Reply to Thread
Results 1 to 5 of 5

Thread: file_get_contents()

  1. #1
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    file_get_contents()

    I am using file_get_contents('http://www.site.com'); to connect to another one of my sites to retrieve information. If for some reason my site is down i need it to prompt the script.

    Example

    $info = file_get_contents('http://www.site.com/updates/index.php');

    If there is no error

    echo $info

    else

    Echo "unable to check updates at this time.";

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: file_get_contents()

    On failure, file_get_contents() will return FALSE.

    You can do something like this:

    PHP Code:

    $info 
    file_get_contents('http://www.site.com/updates/index.php'
      or exit(
    "</br> Unable to check updates at this time.");

    //there is no error, continue

    echo $info

  3. #3
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: file_get_contents()

    I still receive the following errors on the screen that i was hoping to remove with the error code. I did what you showed and it does display Unable to check updates at this time but does not remove theses errors



    Warning
    : file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/e85and/public_html/about.php on line 7

    Warning: file_get_contents(http://www.site.com/updates/index.php) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/e85and/public_html/about.php on line 7



  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: file_get_contents()

    You can prevent the display of non-fatal errors in a webpage with ini_set. Try
    Code:
    ini_set('display_errors', 0);

  5. #5
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: file_get_contents()

    You can also suppress errors for one line using @.

    If you are trying to file_get_contents() from a site outside of your own, consider this (from php.net):

    Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers