2012-08-26 207 views
0

我從某些URL試圖提取網頁的源文件時,得到一個奇怪的錯誤..某些URL返回頁面不在那裏時,它顯然是...捲曲返回404錯誤

這是我的代碼中號使用:

更新:加入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) "" }

+0

試試'curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);'。也許asos.com根據用戶代理阻止請求 – Peter

+0

@PeterSzymkowski - 謝謝 - 現在簡單地返回一個空白頁 –

+0

請顯示'var_dump(curl_info($ ch));''curl_exec'後的結果 – Peter

回答

1

固定的,至少我希望如此:

新增curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");現在一切似乎確定。

非常感謝我收到的幫助。

+0

啊,好!當我查看curl作爲用戶代理髮送的內容時,它與此非常相似。看不到我將它設置在任何地方,但我不能排除某些安裝程序在某處更改了該配置(或者它只是默認配置) –

+0

@JoachimIsaksson非常感謝,非常感謝。 –