【问题标题】:cURL returning 404 errorcURL 返回 404 错误
【发布时间】:2012-08-26 17:13:18
【问题描述】:

在尝试提取页面源时,我从某些 URL 收到一个奇怪的错误。某些 URL 在页面明显存在时返回页面不存在...

这是我正在使用的代码:

更新:添加curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

还是一样的错误

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

$url = 'http://www.asos.com/American-Apparel/American-Apparel-Slim-Slack-Jeans/Prod/pgeproduct.aspx?iid=2503251&cid=4208&sh=0&pge=0&pgesize=20&sort=-1&clr=Truffle';

$html = file_get_contents_curl($url);

 echo $html;

有人知道原因吗?还是有办法解决? file_get_contents 也不起作用,所以我猜同样的错误

var_dump(curl_getinfo($ch));产生

array(22) { ["url"]=> string(67) "http://www.asos.com/holding.html#500?aspxerrorpath=/pgeproduct.aspx" ["content_type"]=> string(9) "text/html" ["http_code"]=> int(200) ["header_size"]=> int(2131) ["request_size"]=> int(437) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(2) ["total_time"]=> float(0.115457) ["namelookup_time"]=> float(4.1E-5) ["connect_time"]=> float(4.1E-5) ["pretransfer_time"]=> float(7.0E-5) ["size_upload"]=> float(0) ["size_download"]=> float(4064) ["speed_download"]=> float(35199) ["speed_upload"]=> float(0) ["download_content_length"]=> float(4064) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(0.047221) ["redirect_time"]=> float(0.067962) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" }

【问题讨论】:

  • 试试curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);。也许 asos.com 会根据 User-Agent 阻止请求
  • @PeterSzymkowski - 谢谢 - 现在只是返回一个空白页
  • 请在curl_exec之后显示var_dump(curl_info($ch));结果
  • @PeterSzymkowski 用我现在正在尝试的代码更新了问题,但仍然遇到同样的错误。
  • 请给我看curl_info($ch)的输出

标签: php parsing curl file-get-contents


【解决方案1】:

已修复,至少我希望如此:

添加了curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");,现在一切正常。

非常感谢我收到的帮助。

【讨论】:

  • 啊,很好!当我检查我的 curl 作为用户代理发送的内容时,它与此非常相似。看不到我在任何地方设置了它,但我不能排除某些安装程序在某处更改了该配置(或者默认情况下它只是这样配置)
猜你喜欢
  • 2014-01-26
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
相关资源
最近更新 更多