我正在尝试登录Google帐户,以便能够在我自己的应用程序中使用Google Ingress Intel地图.我发现了这个问题(
Login to Google with PHP and Curl,Cookie turned off?)并且我可以登录Google帐户,但是当我想加载Ingress Intel地图(
http://www.ingress.com/intel)时出现问题,然后我收到此错误:
Forbidden (403)
CSRF verification Failed. Request aborted.
我知道CSRF,但我不知道我做错了什么.也许是因为我的cookie.txt文件是空的,但为什么呢?
这是我的代码:
$ch = curl_init(); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30); curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie.txt'); curl_setopt($ch,CURLOPT_COOKIEFILE,CURLOPT_HEADER,0); curl_setopt($ch,120); curl_setopt($ch,CURLOPT_TIMEOUT,120); curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage'); $data = curl_exec($ch); $formFields = getFormFields($data); $formFields['Email'] = $USERNAME; $formFields['Passwd'] = $PASSWORD; unset($formFields['PersistentCookie']); $post_string = ''; foreach($formFields as $key => $value) { $post_string .= $key . '=' . urlencode($value) . '&'; } $post_string = substr($post_string,-1); curl_setopt($ch,'https://accounts.google.com/ServiceLoginAuth'); curl_setopt($ch,CURLOPT_POST,CURLOPT_POSTFIELDS,$post_string); $result = curl_exec($ch); curl_setopt($ch,'http://www.ingress.com/intel'); curl_setopt($ch,0); curl_setopt($ch,null); $result = curl_exec($ch); var_dump($result); function getFormFields($data) { if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is',$data,$matches)) { $inputs = getInputs($matches[1]); return $inputs; } else { die('didnt find login form'); } } function getInputs($form) { $inputs = array(); $elements = preg_match_all('/(<input[^>]+>)/is',$form,$matches); if ($elements > 0) { for($i = 0; $i < $elements; $i++) { $el = preg_replace('/\s{2,}/',' ',$matches[1][$i]); if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i',$el,$name)) { $name = $name[1]; $value = ''; if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i',$value)) { $value = $value[1]; } $inputs[$name] = $value; } } } return $inputs; }
感谢您的建议我做错了:)
<?PHP $username = 'email@gmail.com'; $password = 'password'; $cookies = 'cookies.txt'; function getFormFields($data) { if(preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is',$matches)) { $inputs = getInputs($matches[1]); return $inputs; } else { die('didnt find login form'); } } function getInputs($form) { $inputs = array(); $elements = preg_match_all('/(<input[^>]+>)/is',$matches); if($elements > 0) { for($i = 0; $i < $elements; $i++) { $el = preg_replace('/\s{2,$matches[1][$i]); if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i',$name)) { $name = $name[1]; $value = ''; if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i',$value)) { $value = $value[1]; } $inputs[$name] = $value; } } } return $inputs; } $ch = curl_init(); curl_setopt($ch,"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML,like Gecko) Chrome/24.0.1312.56 Safari/537.17"); curl_setopt($ch,$cookies); curl_setopt($ch,0); curl_setopt($ch,'https://accounts.google.com/ServiceLogin?service=ah&passive=true&continue=https://appengine.google.com/_ah/conflogin%3Fcontinue%3Dhttp://www.ingress.com/intel'); $data = curl_exec($ch); $formFields = getFormFields($data); $formFields['Email'] = $username; $formFields['Passwd'] = $password; unset($formFields['PersistentCookie']); $post_string = ''; foreach($formFields as $key => $value) { $post_string .= $key . '=' . urlencode($value) . '&'; } $post_string = substr($post_string,$post_string); $result = curl_exec($ch); curl_setopt($ch,'https://www.ingress.com/intel'); curl_setopt($ch,null); $result = curl_exec($ch); echo $result; curl_close($ch); @unlink($cookies); ?>
原文地址:https://www.jb51.cc/php/134345.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。