PHP卷曲随机挂起

时间:2014-08-29 07:36:47

标签: php curl youtube

我写了一个PHP脚本,卷曲URL以获取页面html。页面内容大约有50%的时间返回,其余时间只返回部分内容,脚本无法终止。我没有收到任何错误......

$headers = array(
    'Accept-Language: en-US,en;q=0.8',
    'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36',
    'Content-Type: application/x-www-form-urlencoded',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.youtube.com/channel/UCkN6ktadXpZl_viwRCSEGUQ');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curl_info = curl_getinfo($ch); 
$response = curl_exec($ch);
curl_close($ch);

print $response;
print_r($curl_info);

在CLI上运行:

php script_name.php

如果跑了10次左右,你会发现它至少无法完成几次而没有任何警告或错误......

1 个答案:

答案 0 :(得分:0)

Ubuntu已经执行了一系列系统更新。更新完成后,我正在使用代码。系统重启后,这个问题就完全消失了。去图。