Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl_exec not returning data

Tags:

php

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;
}
like image 397
Marcin Avatar asked Dec 07 '11 03:12

Marcin


Video Answer


1 Answers

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.

like image 75
Marc B Avatar answered Oct 10 '22 00:10

Marc B