file_get_contents retry on error and slow down

Posted on 2006-04-16
Last Modified: 2013-12-12
I am using file_get_contents to get the contents of many files on a remote server. Occasionally I get a "failed to open stream: HTTP request failed! HTTP/1.1 503 Service Unavailable" error. I would like to write my PHP code to just retry N times when this happens until there is success. I am also worried that my PHP script may be calling up this files too quickly, is there a way to slow down a for loop?

foreach ($links as $page) {
  $contents = file_get_contents($page);
  $descriptions[$i] = strip_tags($contents);

$links is an array of about 2500 pages.
Question by:sypder
    LVL 12

    Accepted Solution


    as for slowing your loop down you can use the sleep function to delay execution by x number of seconds

    foreach ($links as $page) {
      $contents = file_get_contents($page);
      $descriptions[$i] = strip_tags($contents);
      sleep(3); // wait 3 seconds before continuing interations

    the only drawback to this is that (untested opinion here) it may cause your php server to have an execution timeout error if the loop takes longer than the ini file's setting for max code execution time.

    As for your set amount of retries, I would think you could count the number of false values returned by file_get_contents and when it matches the preset, to exit or skip that implementation of the call. If file_get_contents can't read a file, it will return false.

    Simply create a var called $ct and initialze it to zero. then each time file_get_contents returns a false, do a $ct++ and test it agains a preset retry value of say '3' or something you choose.

    Hope this helped :)
    LVL 3

    Author Comment

    That does help. Thanks.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now