【发布时间】:2011-04-11 23:40:37
【问题描述】:
这是我的代码:
$url = 'http://www.douban.com/'; $url = str_replace(" ", "%20", $url); $TheURL_header = substr($url, 0, 7); if ($TheURL_header == "http://") { $pos = strpos($url, "/", 7); 如果($pos){ $host = substr($url, 7, $pos - 7); } 别的 { $host = substr($url, 7); } $referer = "http://" 。 $主机。 "/"; } else if ($TheURL_header == "https://") { $pos = strpos($url, "/", 8); 如果($pos){ $host = substr($url, 8, $pos - 8); } 别的 { $host = substr($url, 8); } $referer = "https://" 。 $主机。 "/"; } 别的 { $pos = strpos($url, "/"); 如果($pos){ $host = substr($url, 0, $pos); } 别的 { $host = substr($url, 0); } $url = "http://" 。 $网址; $referer = "http://" 。 $主机。 "/"; } $c = curl_init(); $curl_header = 数组( '接受: */*', '推荐人:'。 $推荐人, '用户代理: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2', '主持人: ' 。 $主机, '连接:保持活动'); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_HTTPHEADER, $curl_header); curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($c, CURLOPT_TIMEOUT, 30); curl_setopt($c, CURLOPT_HEADER, 0); $res = curl_exec($c); 回声 $res;当我设置 $url = 'http://www.google.com' 时它工作正常,但如果我更改 $url,如 www.aoguejewellery.com 和其他一些 url,我总是收到 403 错误。
【问题讨论】:
-
403不是 Bad Request 错误,而是 Forbidden 错误。 Bad Request 的状态码是400。 -
该站点可能正在使用 cookie 来跟踪会话。我没有把它作为答案,因为我有更多使用 java 抓取网页的经验。但是我遇到了类似的问题,我不得不使用 apache HttpClient java 库来完全模拟浏览器并处理 cookie 管理。只是要研究一下。
-
删除所有 substr/strpos 废话并使用 urlencode() :p
-
无法复制 - 作品。也许您的IP被阻止或其他什么。顺便说一句,在 curl 设置中添加行
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);以在 curl 中启用重定向嗅探。