curl php操作在120000毫秒后超时,接收到234570字节
我的php curl请求正在按预期超时,并给出错误消息:“操作在120000毫秒后超时,收到234570字节” 但是,如何在超时的情况下获取接收到的字节curl php操作在120000毫秒后超时,接收到234570字节,php,http,curl,Php,Http,Curl,我的php curl请求正在按预期超时,并给出错误消息:“操作在120000毫秒后超时,收到234570字节” 但是,如何在超时的情况下获取接收到的字节 $url = "example.com"; $timeout = 120; $ch = curl_init(); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_
$url = "example.com";
$timeout = 120;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$curl_page = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
var_dump($curl_page, $error);
不要使用CURLOPT_RETURNTRANSFER。使用CURLOPT_文件代替,例如
$outfileh=tmpfile();
$outfile=stream_get_meta_data($outfileh)['uri'];
curl_setopt($ch,CURLOPT_FILE,$outfileh);
curl_exec($ch);
$curl_page=file_get_contents($outfile);
(别忘了fclose($outfileh),否则会发生资源泄漏,请记住,使用tmpfile(),fclose()也会为您删除该文件……好消息是,php无论如何都会在执行结束时将其清除)-另一种选择是使用CURLOPT_WRITEFUNCTION,例如
$curl_page = '';
curl_setopt ( $ch, CURLOPT_WRITEFUNCTION, function ($ch, $recieved) use (&$curl_page) {
$curl_page .= $recieved;
return strlen ( $recieved );
} );
curl_exec($ch);
- 它的优点是IO更少,这将在内存中进行内部处理,不像CURLOPT_文件方法,它可能会开始将其写入磁盘,具体取决于OS IO缓存