When I using curl with CURLOPT_FILE
and CURLOPT_RETURNTRANSFER
option, file is empty, without any curl error:
$fp = fopen($saveTo, 'w+');
$ch = curl_init($fileUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'ccc.text');
curl_exec($ch);
Curl error: No error
echo curl_strerror(curl_errno($ch)); //No error
By remove CURLOPT_RETURNTRANSFER
result is true.
I test it in php 5.6 & 7.2 with apache and php-fpm 7.2 with nginx in docker.
CURLOPT_RETURNTRANSFER
needs be set to true
before setting CURLOPT_FILE
to the file handle.
Compare with this comment in the PHP manual :
It appears that setting CURLOPT_FILE before setting CURLOPT_RETURNTRANSFER doesn't work, presumably because CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set.
So try different a order:
$fp = fopen($saveTo, 'wb');
$ch = curl_init($fileUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'ccc.text');
$result = curl_exec($ch);
See also these comments in the PHP manual: CURLOPT_FILE to STDOUT and fclose for additional information on how to restore output to STDOUT and that the file pointer/stream handle needs to be closed after curl finished.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.