入口登录错误

时间:2014-09-02 22:06:34

标签: php curl

同一问题:Login to Google Ingress

我试图在那里发表评论,但它已被删除......不是很好的监控。

我正在尝试使用PHP登录,他们有一个ALMOST工作的解决方案,但并不完全。第一个解决方案登录到Google Auth就好了,但加载Ingress页面的页面仍然只是说“登录”

我们如何修改代码以完成正确的登录?

谢谢!

<?php
$username = 'email@gmail.com';
$password = 'password';
$cookies = 'cookies.txt';

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', $el, $value)) {
                                        $value = $value[1];
                                }
                                $inputs[$name] = $value;
                        }
                }
        }
        return $inputs;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

curl_setopt($ch, CURLOPT_URL, '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, 0, -1);

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);

curl_setopt($ch, CURLOPT_URL, 'https://www.ingress.com/intel');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, null);
$result = curl_exec($ch);

echo $result;

curl_close($ch);
@unlink($cookies);
?>

0 个答案:

没有答案