我知道curl不執行javascript,它只抓取靜態html,所以這就是爲什麼簡單的curl不適合我的原因。 我對PHP知之甚少,我對此很陌生,但我的理解是,如果我不必先登錄以獲取內容,我可以簡單地使用file_get_contents女巫將首先執行動態內容,然後抓住html內容,女巫回報給我我需要的,但我首先必須登錄,然後獲得頁面。 我試着用捲曲使用curl登錄後得到動態生成的內容
$user = "myuser";
$pass = "mypassword";
//create cookie file
$random = rand(0,9999999);
$cookie = $random."cookie.txt";
$fp = fopen("$cookie","w") or die("<BR><B>Unable to open cookie file $cookie_file_path for write!<BR>");
fclose($fp);
//do login using curl
$LOGINURL = "https://controlpanel.example.com/index.html";
$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 Firefox/29.0";
$v2 = array('userName'=>$user, 'password'=>$pass);
$reffer = "https://www.google.com";
//this first call is to set the cookie
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
ob_start(); // Prevent output
curl_exec ($ch);
ob_end_clean(); // End preventing output
curl_close ($ch);
unset($ch);
//now that the cookie is set, do login
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$v2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$result = curl_exec($ch);
//now we are logged-in
//now grab the page you need
$profileurl = 'https://controlpanel.example.com/information.html';
curl_setopt($ch, CURLOPT_URL, $profileurl);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec ($ch);
登錄但這隻會得到靜態html,而不是動態的內容了。 讓我更好地解釋。 代碼我得到的,在這一點上使用上述捲曲的方法,在$結果是:
.....
<div id="DisplayAccountInfo"><span class="loading">Loading info</span></div>
.....
如果我做到這一點使用Firefox和Firebug的檢查元素手工來源是:
.....
<div id="DisplayAccountInfo">
<div class="formModule" id="formContainer">
......
<legend>Your code for this hour is 8T5D9LO</legend>
.....
</div>
</div>
.....
什麼我在Firebug控制檯注意的是:
GET https://controlpanel.example.com/async/information.html
200 OK
669ms
jquery-....min.js (line 19)
我,作爲一個菜鳥,從這個不解的是,內容是使用jQuery dinamicly加載,和捲曲不知道該怎麼做。
我試圖把代替
$profileurl = 'https://controlpanel.example.com/information.html';
curl_setopt($ch, CURLOPT_URL, $profileurl);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec ($ch);
//replaced the above with this
$result = file_get_contents($profileurl);
,但我從登錄頁面的HTML,因爲我覺得現在不承認,我登錄。
所以,我怎麼能解決這個問題?你能幫我麼?
你明白錯誤,file_get_contents將不會執行javascript。沒有內置到PHP的意志。你的選擇是1)手動解析返回的源代碼,找出javascript端點並直接調用它們或2)安裝像phantomjs這樣的無頭瀏覽器,它執行javascript – Steve 2014-08-28 10:30:16
這個問題被標記爲jquery,你可以使用jQuery嗎?如果是這樣的話,你可以考慮使用http://api.jquery.com/jQuery.post/ – Kyborek 2014-08-28 10:31:41
user574632簡單的解決方案我看到人們傾向於重定向到phantomjs,但我該如何使用它?我正在查看文檔,但我不知道如何使用它。 – Dan 2014-08-28 10:40:02