Guest User

Untitled

a guest
Feb 15th, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. <?php
  2.  
  3. $USERNAME = 'your_gmail_id_name@gmail.com';
  4. $PASSWORD = 'your_gmail_password';
  5. $COOKIEFILE = 'cookies.txt';
  6.  
  7. // initialize curl handle used for all requests
  8. $ch = curl_init();
  9.  
  10. // set some options on the handle
  11. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  12. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  15. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  16. curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE);
  17. curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
  18. curl_setopt($ch, CURLOPT_HEADER, 0);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  20. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
  21. curl_setopt($ch, CURLOPT_TIMEOUT, 120);
  22.  
  23. // url of our first request fetches the account login page
  24. curl_setopt($ch, CURLOPT_URL,
  25. 'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');
  26. $data = curl_exec($ch);
  27.  
  28. // extract form fields from account login page
  29. $formFields = getFormFields($data);
  30.  
  31. // inject email and password into form
  32. $formFields['Email'] = $USERNAME;
  33. $formFields['Passwd'] = $PASSWORD;
  34. unset($formFields['PersistentCookie']);
  35.  
  36. $post_string = http_build_query($formFields); // build urlencoded POST string for login
  37.  
  38. // set url to login page as a POST request
  39. curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
  40. curl_setopt($ch, CURLOPT_POST, 1);
  41. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  42.  
  43. // execute login request
  44. $result = curl_exec($ch);
  45.  
  46. // check for "Redirecting" message in title to indicate success
  47. // based on your language - you may need to change this to match some other string
  48. if (strpos($result, '<title>Redirecting') === false) {
  49. die("Login failed");
  50. var_dump($result);
  51. }
  52.  
  53. // login likely succeeded - request account page; unset POST so we do a regular GET
  54. curl_setopt($ch, CURLOPT_URL, 'https://mail.google.com/mail/h/jeu23doknfnj/?zy=e&f=1');
  55. curl_setopt($ch, CURLOPT_POST, 0);
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, null);
  57.  
  58. // execute request for login page using our cookies
  59. $result = curl_exec($ch);
  60.  
  61. echo $result;
  62.  
  63.  
  64. // helpef functions below
  65.  
  66. // find google "#gaia_loginform" for logging in
  67. function getFormFields($data)
  68. {
  69. if (preg_match('/(<form.*?class=.?RFjuSb.*?</form>)/is', $data, $matches)) {
  70. $inputs = getInputs($matches[1]);
  71.  
  72. return $inputs;
  73. } else {
  74. die('didn't find login form');
  75. }
  76. }
  77.  
  78. // extract all <input fields from a form
  79. function getInputs($form)
  80. {
  81. $inputs = array();
  82.  
  83. $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
  84.  
  85. if ($elements > 0) {
  86. for($i = 0; $i < $elements; $i++) {
  87. $el = preg_replace('/s{2,}/', ' ', $matches[1][$i]);
  88.  
  89. if (preg_match('/name=(?:["'])?([^"'s]*)/i', $el, $name)) {
  90. $name = $name[1];
  91. $value = '';
  92.  
  93. if (preg_match('/value=(?:["'])?([^"'s]*)/i', $el, $value)) {
  94. $value = $value[1];
  95. }
  96.  
  97. $inputs[$name] = $value;
  98. }
  99. }
  100. }
  101.  
  102. return $inputs;
  103. }
  104. ?>
  105.  
  106. $formFields['Email'] = $USERNAME;
  107. $formFields['Passwd'] = $PASSWORD;
  108. unset($formFields['PersistentCookie']);
Add Comment
Please, Sign In to add comment