6

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;
}

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the tip about not repeatedly instantiating the curl object. As for errors, I did try that and I'm not getting any errors.
Does hitting the urls in a browser work? Perhaps they're blocking curl user agents and the like.
Marc, that made me think of what was wrong! I had a base class for checking if a user session exists. The curl wasn't getting access to a page that requires a session. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.