Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

php - curl_exec not returning data

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...