For some reason the code below when I iterate through some urls, curl_exec never returns anything. I've verified the urls it is getting are correct. I've manually checked them to see if there is output. I've tried removing CURLOPT_RETURNTRANSFER, and curl_exec would return true. I'm not sure why curl_exec isn't returning the data that I need.
function _curl_get($urls)
{
$html_str = '';
foreach ($urls as $url)
{
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$html_str .= curl_exec($curl_handle);
curl_close($curl_handle);
}
return $html_str;
}
Check for errors:
$data = curl_exec($curl_handle);
if ($data === FALSE) {
die(curl_error($curl_handle));
} else {
$html_str .= $data;
}
Never assume that an operation that depends on an external resource succeeded. There's only ONE way things can go right, and literally hundreds/thousands of ways for things to go wrong. Assuming that the 1:100 or 1:1000 chance occured is a bad way to go.
On a slight efficiency note, there's no need to repeatedly instantiate/close a curl object. You CAN reuse it for multiple urls. Especially since they're all being fetched the same way. Create one curl handle outside of the loop, re-use it repeatedly inside the loop (set the url each time), then close it after the loop finishes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With