[請益] cURL模擬登入

看板PHP作者 (史雷斯)時間8年前 (2016/03/23 17:51), 8年前編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/1
各位版友好, 小弟現在想實現的功能是用PHP來模擬登入Office 365, 讓使用者能夠避開輸入登入資訊的頁面直接進到登入後的畫面, 藉以達到假Single sign-on(?的效果。 爬了一下文後似乎可以用PHP的CURL來達成我要的效果, 就找了拿來登入Facebook的範例來改看看, 以下是目前的程式碼。 ----------------------------------------- <?php $login_email = '****'; $login_pass = '****''; $ch = curl_init(); $requestID = create_guid(); $request_headers = array('client-request-id: '. $requestID,); curl_setopt($ch, CURLOPT_URL, "https://login.microsoftonline.com/login.srf?wa=wsignin1.0&rpsnv=4&ct=1458714902&rver=6.7.6640.0&wp=MCMBI&wreply=https%3a%2f%2fportal.office.com%2flanding.aspx%3ftarget%3d%252fdefault.aspx&lc=1028&id=501392&msafed=0"); curl_setopt($ch, CURLOPT_POSTFIELDS,'login='.urlencode($login_email).'&passwd='.urlencode($login_pass)); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);// 在發起連接前等待的時間,如果設 置為0,則不等待 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//是否抓取轉址、是否抓取跳轉後 的頁面 curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__). '/cookie.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__). '/cookie.txt'); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); curl_setopt($ch, CURLOPT_REFERER, "https://portal.office.com/"); $response = curl_exec($ch); function create_guid($namespace = '') { static $guid = ''; $uid = uniqid("", true); $data = $namespace; $data .= $_SERVER['REQUEST_TIME']; $data .= $_SERVER['HTTP_USER_AGENT']; $data .= $_SERVER['LOCAL_ADDR']; $data .= $_SERVER['LOCAL_PORT']; $data .= $_SERVER['REMOTE_ADDR']; $data .= $_SERVER['REMOTE_PORT']; $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data))); $guid = '{' . substr($hash, 0, 8) . '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12) . '}'; return $guid;} //curl_close($ch); ?> ----------------------------------------------- 執行結果:出現登入錯誤 先感謝各位版友! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.96.211 ※ 文章網址: https://www.ptt.cc/bbs/PHP/M.1458726669.A.6FA.html 自己摸了了一晚,知道自己缺了GUID的Hearder之後再試, 目前是可以看到登入畫面顯示登入錯誤, 但還是弄不出來 Orz ※ 編輯: Slas (60.248.96.211), 03/24/2016 14:37:39

03/24 18:38, , 1F
如果你是想讓php去抓html再吐給user,那你走錯方向了
03/24 18:38, 1F

03/24 18:40, , 2F
例如網頁內有用到ajax,你要怎樣繞過same origin policy?
03/24 18:40, 2F

03/25 16:27, , 3F
嗯...爬了兩天的文我已經知道我的方向整個錯誤了
03/25 16:27, 3F

03/25 16:27, , 4F
不過還是感謝回應!
03/25 16:27, 4F
文章代碼(AID): #1MycSDRw (PHP)