PHP 類比登入出現問題
在網上看了一編文章類比人人網登入,恰好現在需要用到,但是出現了 “The URL has moved here” 是什麼原因啊。謝謝,代碼如下:
PHP code
$login_url = 'http://passport.renren.com/PLogin.do'; $post_fields['email'] = [email protected]'; $post_fields['password'] = 'xxx'; $post_fields['origURL'] = 'http://www.renren.com/indexcon'; $post_fields['domain'] = 'renren.com'; //cookie檔案存放在網站根目錄的temp檔案夾下 $cookie_file = tempnam('./temp','cookie'); $ch = curl_init($login_url); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_exec($ch); curl_close($ch); //帶上cookie檔案,訪問人人網首頁 $send_url='http://home.renren.com/Home.do'; $ch = curl_init($send_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $contents = curl_exec($ch); curl_close($ch); //清理cookie檔案 unlink($cookie_file); //輸出人人網首頁的內容 print_r($contents);
------解決方案--------------------
以下內容轉貼.
這兩天類比瀏覽器提交登入表單,提交完成後, The URL has movedhere 。。
每次都需要點擊串連後才開啟登入後的首頁。這並不是我想要的。
又看了一遍說明,看到參數-L
如下:-L/--location Follow Location: hints (H)
--location-trusted Follow Location: and send auth to other hosts (H)
有點衝動,感覺這就是問題的關鍵 :
curl --output "./rr.html" --dump-header "d_cookie01" --cookie-jar "c_cookie01" --create-dirs --location --data "email=test&password=test&autoLogin=ture&origURL=&domain=renren.com&formName=&method=&isplogin=true&submit=登入" http://www.renren.com/PLogin.do
wowowo登入後的首頁下載下來了!!
仔細查看d_cookie01(包含從c_cookie01的內容,但--cookie-jar 產生的更簡潔)有這麼一條
Location:http://www.renren.com/callback.do?t=9d5201d04d44156fb070037e9493f5fd3&origURL=http%3A%2F%2Fwww.renren.com%2FHome.do&needNotify=false
原來每次提交登入表單後還需要跳轉的,而-L 則是跟隨跳轉連結的。
我們也可以分成兩步去做:
1.先提交表單,儲存返回來的cookie
curl --output "./rr.html" --dump-header "d_cookie01" --data "email=test&password=test&autoLogin=ture&origURL=& domain=renren.com&formName=&method=&isplogin=true& submit=登入" http://www.renren.com/PLogin.do
2.在d_cookie01中找到location,然後把cookie和location一起提交
curl --output "./rr.html" --dump-header "d_cookie01" --location http://www.renren.com/callback.do?t=9d5201d04d44156fb070037e9493f5fd3&origURL=http%3A%2F%2Fwww.renren.com%2FHome.do&needNotify=false
OK,就拿到了登入後的首頁。