$query = "https://api.facebook.com/method/fql.query?format=JSON&query=";
$urls = array('about 500 link');
foreach ($urls as $url)
{
$query .= urlencode("select post_fbid, fromid, object_id, text, time from comment where object_id in (select comments_fbid from link_stat where url ='$url')");
$query .= "&pretty=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_TIMEOUT ,900);
$data = curl_exec($ch);
$response = json_decode($data, true);
echo '<pre>';print_r($response);echo '</pre>';
curl_close($ch);
}
当我运行此代码时,$urls
数组中有大约 500 个链接,结果出现错误:ERROR 500 - 内部服务器错误
,但是$urls
仅包含 5 个链接,没有错误。如何在不超时的情况下获取所有 500 个链接的内容?
最佳答案
这不是curl错误,而是您的服务器错误,因为请求持续时间有限制。请求 500 个页面需要一段时间,并且您的脚本将终止。请尝试以下操作:
- 设置
set_time_limit(200);
或更多 - 使用 SSH 连接运行脚本,完全没有超时
- 使用
curl_multi_init
并行执行多个请求
关于php - 使用CURL php获取数据时出现错误500内部服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13876851/