Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Reusing a cUrl context after doing a PUT request in PHP?

Tags:

php

curl

I have some code where I'm trying to reuse a curl context to do put requests and get requests. After each put request the get request fails with this PHP warning:

curl_exec(): CURLOPT_INFILE resource has gone away, resetting to default

I could use the PHP shutup operator, but I would rather properly reset the curl context. Does anyone know how to do this? I could also use different curl contexts, but I would rather reuse the connection since the application is sending a lot of requests. I could keep the file handle open, but that seems like a hackish solution, especially since this is all wrapped up in functions so i can call doPut, doGet, etc

$curlContext = curl_init();
$fh = fopen('someFile.txt', 'rw');
curl_setopt($curlContext, CURLOPT_URL, $someUrl);
curl_setopt($curlContext, CURLOPT_PUT, TRUE);
curl_setopt($curlContext, CURLOPT_INFILE, $fh);
curl_setopt($curlContext, CURLOPT_INFILESIZE, $size);
$curl_response1 = curl_exec($curlContext);
fclose($fh);
curl_setopt($curlContext, CURLOPT_PUT, FALSE);
curl_setopt($curlContext, CURLOPT_HTTPGET, TRUE);
curl_setopt($curlContext, CURLOPT_URL, $someOtherUrl);
$curl_response1 = curl_exec($curlContext);

Thanks.

like image 263
razzard Avatar asked Mar 27 '12 19:03

razzard


1 Answers

Starting from PHP 5.5, curl_reset can be used to reset all previous set options.

For PHP < 5.5, Li-chih Wu's solution is a possible workaround.

like image 64
K.C. Tang Avatar answered Sep 22 '22 19:09

K.C. Tang