我對cURL很新,並且只使用它很短的時間。 我的問題是,我想通過使用cURL來獲取頁面的內容(file_get_content()
不起作用)。不幸的是,有問題的網站有機器人保護,這意味着當你第一次到達該網站時,它會檢查你是否是機器人。如果你不是機器人,它會用絕對路徑將你重定向到真實網站(我猜)。 每當我用cURL加載這個站點時,它會將路徑追加到我的服務器地址。curl把重定向的url放到瀏覽器的adressline中
例如: 我的服務器地址:http://examplepage.com/
cURL將重定向的路徑附加到我的URL。所以它會是這樣的:http://examplepage.com/absolute/path?with=parameters
在原始頁面上,我試圖從中獲取內容,它的工作原理是因爲他們有這樣的路徑,但我不(我想要一些html內容的網站) 。
這是到目前爲止我的代碼:
<?php
/* getting site */
$website = "https://originalsite.com/?some=parameters";
$redirectURL;
function curl_download($url) {
//initialize curl handler
$c = curl_init();
// Include header in result? (0 = yes, 1 = no)
curl_setopt($c, CURLOPT_HEADER, 1);
//set url to download
curl_setopt($c, CURLOPT_URL, $url);
// follow redirection
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
//set referer
curl_setopt($c, CURLOPT_REFERER, "https://originalsite.com/");
// User agent
curl_setopt($c, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
// Timeout in seconds
curl_setopt($c, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($c);
// Close the cURL resource, and free system resources
curl_close($c);
return $output;
}
$content = curl_download($website);
echo $content;
?>
所以它會進入網站,它檢查自己是否是一個殭屍或不和之後,它重定向我的網站(或它至少,它嘗試)。
我已經搜索了互聯網和StackOverflow,但我找不到我的問題的答案。
當你print_r()你的'$ output'時會發生什麼? – KDOT
沒什麼不同。 – CeyKie
好吧,你肯定會有一個錯誤封裝或$'output'的一些響應。你可以添加'$ output'的值是什麼嗎?整個想法是調試請求。 – KDOT