我使用
PHP CURL来解析负载很重的站点(这个站点甚至很少在浏览器中打开).在结果中,我有服务器响应代码503或0(没有).
也许您可以给我建议或告诉我一些CURL功能,以获得正常的服务器响应?
有我的CURL选项:
$options = array(
CURLOPT_REFERER => $url,
CURLOPT_TIMEOUT => 1800,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADERFUNCTION => "curlHeaderCallback",
CURLOPT_COOKIE => Cookies::arrayToString(Cookies::instance()->load()),
CURLOPT_USERAGENT => "Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.50",
CURLOPT_HTTPHEADER => $headers
);
问题是我无法通过页面代码获得响应.
我有2个变种:
1.服务器没有回答;
2.在服务器的回答中,我得到代码为503的页面“服务器超载”.
CurlHeaderCallback()代码:
`function curlHeaderCallback($ch, $str)
{
if (strncmp($str, "Set-Cookie:", 11) === 0)
{
$cookie = trim(substr($str, 11));
list($cookie, $options) = explode(";", $cookie, 2);
list($key, $value) = explode("=", $cookie, 2);
Cookies::instance()->set($key, $value);
}
if (trim($str) == "")
{
curl_setopt($ch, CURLOPT_COOKIE, Cookies::arrayToString(Cookies::instance()->load()));
}
return (strlen($str));
}`
我的行动是:
$response = curl_exec($ch);
$info = curl_getinfo($ch);
我没有回复和$info [“http_code”]或第二个变体:作为回应我有第503页代码和$info [“http_code”] = 503
哦,还有一个选择是:
CURLOPT_CONNECTTIMEOUT => 30
图表在这里:http://s61.radikal.ru/i172/1212/d6/33471472ee8e.png
最佳答案 如果您刚刚使用http代码,则需要使用CURLINFO_HTTP_CODE使用
curl_getinfo,例如;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);