tags:

views:

14

answers:

1

Someone had asked me to help him with cURL today, and as a result I looked up the function that I written a while ago. I was wondering why I used the ob functions. I probably followed some tutorial at the time; however, when I look at most cURL classes now they don't use the ob functions... I suppose the question is, what is better performance wise? Is there anything wrong with the code below? and Is there a better way to do this?

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
+1  A: 

By default, curl outputs the response to standard out. So, your code buffers the output, then accesses it with ob_get_contents.

You should use CURLOPT_RETURNTRANSFER instead. That causes curl to return the response from curl_exec.

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$string = curl_exec ($ch);
curl_close ($ch);
return $string;
Matthew Flaschen