問題概述:通過php curl模擬登陸一個網(wǎng)站如http://www.aaa.com,通過fiddler抓包分析如下:1、表單以POST方式提交到http://www.aaa.com/dologin,這里產(chǎn)生了一個token:xxx,2、服務(wù)器帶著這個token跳轉(zhuǎn)到了以下地址來登陸:https://account.usercenter.com/login?token=xxx&target_url=http://www.aaa.com;(注意域名不同,并且是https,此外這個攜帶token的url拷貝到任何電腦都能正常登陸,登陸成功后就會失效)3、登陸成功后地址重定向到target_url:http://www.aaa.com
問題分析:我的理解:有一臺授權(quán)服務(wù)器,任何一臺PC電腦訪問攜帶有效token的url,PC和服務(wù)器之間通過cookie來保持這個token;
提出問題:使用php curl該如何實(shí)現(xiàn)這個登陸模擬?
以下是我的代碼:
<?php
$cookie_file = 'E:\work\cookie.txt';
$login_url = 'http://www.aaa.com/dologin';
$post_fields = 'userName=aa&password=bb&service_key=cc'
$post_fields.= '&callback_url=http%3A%2F%2Fwww.aaa.com&hostUrl=http%3A%2F%2Fwww.aaa.com';
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$contents=curl_exec($ch);
curl_close($ch);
preg_match('/(https:\/\/account\.usercenter\.com\/tokenLogin[^\s]*)\s*/',$contents,$match);
//var_dump($match);die; 此處匹配出攜帶token的url
$ch = curl_init($match[1]);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36 OPR/41.0.2353.46");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
$url='http://www.aaa.com/1.html';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
//curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36 OPR/41.0.2353.46");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
var_dump($contents);//這里輸出的頁面顯示沒有登陸成功(這里是問題所在)
?>
不知通過cookie能不能實(shí)現(xiàn)這種登陸?各位大俠請指點(diǎn)~~
添加回答
舉報
0/150
提交
取消