PHP 類比登入出現有關問題

來源:互聯網
上載者:User
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,就拿到了登入後的首頁。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.