[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

file_get_contents retry on error and slow down

Posted on 2006-04-16
2
Medium Priority
?
644 Views
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);
  $i++;
}

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

Accepted Solution

by:
Richard Davis earned 1500 total points
ID: 16467135
hello,

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);
  $i++;
  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 :)
0
 
LVL 3

Author Comment

by:sypder
ID: 16467323
That does help. Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

830 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