Vontio
Vontio

Reputation: 311

curl_close will clean up the return result of curl_exec

I surely confirm the result of curl_exec will be cleaned up by curl_close.
I have to comment out the curl_close line to get the result.My php version is 5.3.8.
How do I get result with curl_close? Here is my code

function curl_get_contents($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $r = curl_exec($ch);
    //curl_close($ch);
    return $r;
}

Upvotes: 1

Views: 4592

Answers (1)

Mob
Mob

Reputation: 11098

It has no effect on the return value, as long as the data from curl_exec(); is stored in $r you can return as you like.

This works normally.

function curl_get_contents($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
 }

$returnedValue = curl_get_contents($url); //Holds the contents

Edit as Marc B pointed out :

You don't need to do a curl close. PHP will clean up for you when the function returns and $ch goes out-of-scope.

Hence there's no point of even closing it, but it shouldn't happen.

Upvotes: 3

Related Questions