Reputation: 53
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.
Upvotes: 5
Views: 3285
Reputation: 139
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.
Upvotes: 3
Reputation: 95101
You can simply use curl_setopt_array
instead of re using the context
$file = 'log.txt';
$fh = fopen($file, 'rw');
$options = array(
CURLOPT_URL => 'http://localhost/lab/stackoverflow/b.php',
CURLOPT_PUT => 1,
CURLOPT_INFILE => $fh,
CURLOPT_INFILESIZE => filesize($file),
CURLOPT_HEADER => false
);
// First Request
curl_setopt_array($ch = curl_init(), $options);
echo curl_exec($ch);
fclose($fh);
// Secound Request
$options[CURLOPT_URL] = "http://localhost/lab/stackoverflow/c.php";
unset($options[CURLOPT_INFILE], $options[CURLOPT_INFILESIZE]);
curl_setopt_array($ch = curl_init(), $options);
echo curl_exec($ch);
Upvotes: 0
Reputation: 1082
After fclose($fh), do curl_setopt($curlContext, CURLOPT_INFILE, STDIN);
Will avoid the "CURLOPT_INFILE resource has gone away, resetting to default" warning.
Upvotes: 2