这是一些经过修改的代码。
它首先请求登录页面获取初始cookie,并提取登录表单所需的值。接下来,它执行登录服务的发布。然后,它检查是否正在尝试使用javascript和meta标签重定向到目标URL。
似乎您已经具有获取表单字段的代码,所以我没有发布我的代码,但是如果您需要它,请告诉我。只要确保
$formFields是一个关联数组,其中键是字段名,值是字段值。
<?php$USERNAME = 'youraccount@gmail.com';$PASSWORD = 'password';$cookieFILE = 'cookies.txt';// initialize curl handle used for all requests$ch = curl_init();// set some options on the handlecurl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0");curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_cookieJAR, $cookieFILE);curl_setopt($ch, CURLOPT_cookieFILE, $cookieFILE);curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);curl_setopt($ch, CURLOPT_TIMEOUT, 120);// url of our first request fetches the account login pagecurl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');$data = curl_exec($ch);// extract form fields from account login page$formFields = getFormFields($data);// inject email and password into form$formFields['Email'] = $USERNAME;$formFields['Passwd'] = $PASSWORD;unset($formFields['Persistentcookie']);$post_string = http_build_query($formFields); // build urlenpred POST string for login// set url to login page as a POST requestcurl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);// execute login request$result = curl_exec($ch);// check for "Redirecting" message in title to indicate success// based on your language - you may need to change this to match some other stringif (strpos($result, '<title>Redirecting') === false) { die("Login failed"); var_dump($result);}// login likely succeeded - request account page; unset POST so we do a regular GETcurl_setopt($ch, CURLOPT_URL, 'https://myaccount.google.com/?utm_source=OGB');curl_setopt($ch, CURLOPT_POST, 0);curl_setopt($ch, CURLOPT_POSTFIELDS, null);// execute request for login page using our cookies$result = curl_exec($ch);echo $result;// helpef functions below// find google "#gaia_loginform" for logging infunction 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'); }}// extract all <input fields from a formfunction 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;}


