2
假設我想從中獲取數據的頁面需要登錄...如何使用file_get_contents獲取該頁面上的數據?需要登錄的頁面上的file_get_contents
假設我想從中獲取數據的頁面需要登錄...如何使用file_get_contents獲取該頁面上的數據?需要登錄的頁面上的file_get_contents
一個選項是使用curl來獲取所需的數據。
$url = "http://site.com/url-to-post-data-to";
$postfields = "var1=data&var2=data";
$password = "password"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $password);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$response = curl_exec($ch);
echo $response;
CURL將是一個更好的方式來做到這一點,但如果你有使用的file_get_contents,然後使用類似的用戶/密碼的東西任何「高級」選項HTTP,你必須先創建一個流上下文。這是PHP docs page here上的一個例子,例子#4。
什麼樣的登錄? –
curl浮現在腦海,當然你有權從網站所有者那裏做到這一點 – 2011-02-13 19:09:52
可能你需要使用file_get_contents + stream_context_create發送一個POST請求來發送你的登錄憑據,從$ http_response_header獲取發送的cookie並使用anohter file_get_contents + stream_context_create使用此cookie訪問目標頁面。 – NikiC