I have a quite long loop running in a function , but does not finish all the eventual iterations and stops without giving any errors :
function my_function(){
foreach (range(100,999) as $art_id){
$current++;//see bottom flush functions...
outputProgress($current, $art_id );//see bottom flush functions...
// do a lot of stuff on remote URL...
// including download images ,
// scraping HTMl etc ..
}
}
I am using some output progress with flush to track the progress
function outputProgress($current, $total) {
// echo "<span style='background:red;font-size:1.5em;color:#fff;'>" . round($current / $total * 100) . "% </span>";
echo "<span style='background:red;font-size:1.5em;color:#fff;'>" . $current .'/'. $total . "% </span>";
myFlush();
sleep(1);
}
and
function myFlush() {
echo(str_repeat(' ', 256));
if (@ob_get_contents()) {
@ob_end_flush();
}
flush();
}
(never mind the percentage calculation , it is now disabled and just shows the ID of iteration)
I have noticed that most of the time I am executing the loop, It will just STOP after 20-25 iterations . sometimes as little as 10.
My first suspects were the time limit , and max_execution time, so I added :
set_time_limit(99999);
ini_set('max_execution_time', 99999);
function my_function(){
foreach (range(410,499) as $art_id){ // done 500-600
set_time_limit(99999);
ini_set('max_execution_time', 99999);
// do a lot of stuff on remote URL...
// including download images ,
// scraping HTMl etc ..
}
}
As you can see, I have added those both INSIDE and OUTSIDE the function itself, just in case .
But it does not help much , and the loop still stops .
My next suspect was the Memory limit, so I added :
ini_set('memory_limit','128M');
and since I am working on wp, I also tried
define('WP_MEMORY_LIMIT', '128M');
but to no avail. The scipt still stops after little iterations .
- What are the other possible causes for this behavior , and the possible remedies ?
Mind you - the script does not give any errors, it just stops at a certain loop.
EDIT I
I have pasted the script HERE . it is actually a slightly modified scrap_slashdot() function from the simplehtmldom lib included examples.
It is modified to insert wordpress posts while also downloading images and attaching them.
EDIT II
Using @Allendar comment echo ini_get('memory_limit'); seems like it works and it is set to 128M..
error_reporting(E_ALL); ini_set('display_errors', '1');and see if there is an error. If your script is failing due to one of the problems you suspect, the interpreter will tell you. also, try not to suppress errors using@. You may also not be able to useini_setin a shared environment.foreach (range(100,999) as $art_id)->$range = range(100, 999); foreach($range as $ard_id)or just do a for loop like thisfor($i=100;$i<1000;$i++){if(!ini_set("max_execution_time", 99999)){ echo "failed";exit;}If you get "failed" then it means that ini_set() doesn't work for some reason ...