PHP CURL inside while loop performance
I have written a CURL handler which is being called inside a WHILE loop. After executing each and every CURL exec, I am freeing up the object resources by setting the CURL handler to NULL. But even though after cleaning up the request also, I am getting a weird error after executing 80K calls like,
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 13471 bytes)
And also, I want to know, suppose If a CURL request throws a timeout error, will the error be cached inside the loop?. Why I am asking is, I am trying to execute a API call using the CURL handler. If a API calls fails for the first time, it fails forever even though it gets executed multiple times.
eg: I am tring to call a facebook search API and it returns a error like,
connect() timed out!
After executing the first time, if I try to run next time or more than anytime, again it returns the same error.
So, can anyone please help me out of this.
NOTE:
- I have an idea to use CURL_MULTI_EXEC, but before implementing I need some suggestions from more people like, how to handle the above two cases and how to manage the CURL object resources.
- How to clean up the cache from CURL?
Here is my code,
while(true) {
$curlObj = triggerCurl($serachUrl);
return $curlObj;
}
function triggerCurl($url) {
$url = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set the default options
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CU开发者_Python百科RLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
$ret = curl_exec($ch);
curl_close($ch);
unset($ch);
}
精彩评论