0

I am calling this function from my browser through a controller:

function writePages($startPage)
{
    for ($page=$startPage;$page<90;$page++)
    {
        $content=file_get_contents('http://www.websiteIamUsing.com/'.$page);
        $handle=fopen('mytxtfile'.$page.'.txt','w');
        fwrite($handle,$content);
        fclose($handle);
    }
}

For some reason, this will write the first 20-something, or sometimes 50-something files, but then it will stop and give me this error:

Warning: get_class() expects parameter 1 to be object, resource given in 
/Applications/MAMP/htdocs/mysite/lib/Cake/Utility/Debugger.php on line 579

Warning: get_object_vars() expects parameter 1 to be object, resource given in 
/Applications/MAMP/htdocs/mysite/lib/Cake/Utility/Debugger.php on line 585

Warning: Invalid argument supplied for foreach() in 
/Applications/MAMP/htdocs/mysite/lib/Cake/Utility/Debugger.php on line 586

Warning: ReflectionObject::__construct() expects parameter 1 to be object, resource 
given in /Applications/MAMP/mysite/alexmohamed/lib/Cake/Utility/Debugger.php 
on line 592

Fatal error: ReflectionClass::getProperties() 
[<a href='http://php.net/reflectionclass.getproperties'>reflectionclass.getproperties</a>]: 
Internal error: Failed to retrieve the reflection object in 
/Applications/MAMP/htdocs/mysite/lib/Cake/Utility/Debugger.php on line 594

Can anyone explain to me what is going on here? Since it begins to work, I suspect maybe CakePHP doesn't let you run a script for longer than a certain amount of time or something? Is there a way around this? This is a controller action that only I will use...it won't be used by the users of the site so it can take a long time (this one should take about a minute).

Edit: If you look at the errors, it appears to be trying to debug something, but since cakePHP is passing a "resource" rather than an "object" to the debugger, it won't tell me the real error. Why would this be an unreliable action...sometimes it completes until the end, sometimes it stops after ~20 files, sometimes after ~50...

4

2 回答 2

0

Well, Debugger is not the one who gives out the error, but usually when something is wrong, cake attempts to print out some variables for you to see and that's why debugger is complaining (when it can't print out the variable correctly)

Also, file_get_contents does not always guarantee you with the data you wanted in perhaps the page size is too large (memory exhaustion), or the remote server fails/blocks certain requests especially you ran a for-loop so quickly.

One way you could try is probably using curl to crawl the pages?

(Reference from: php file_get_contents fails sometimes during cronjob)

$fh = fopen('mytxtfile'.$page.'.txt', 'w'); 
$ch = curl_init('http://www.websiteIamUsing.com/'.$page); 
curl_setopt($ch, CURLOPT_FILE, $fh);
#curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //in case remote server is in https and you want to skip the ssl verification
#curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //in case remote server is in https and you want to skip the ssl verification
curl_exec($ch); 
curl_close($ch);
fclose($fh);
# usleep(500000); and perhaps a 500ms sleeping gap?

Try and see if you get better result or not ;)

于 2013-09-21T10:32:20.217 回答
0

Thanks everyone...both suggestions were helpful. I upgraded to 2.4.1 and Cake didn't send a resource to the debugger anymore, so I was able to see the real error which was simply that file_get_contents failed. So I changed the for loop into a while loop like so:

$page=1;
$pages=90;
while ($page<=$pages)
{
    if($content=file_get_contents('http://www.websiteIamUsing.com/'.$page))
    {
        $handle=fopen('mytxtfile'.$page.'.txt','w');
        fwrite($handle,$content);
        fclose($handle);
        $page++;
    }
}

There are still errors because file_get_contents fails randomly every so often, but then it will keep retrying until it works, and now even though there are errors, I always end up with the final result I wanted. If anyone can shed insight onto why this command is unreliable, let me know.

于 2014-09-30T15:06:14.323 回答