Foros del Web » Programando para Internet » PHP »

Acceso a cuenta google con CURL

Estas en el tema de Acceso a cuenta google con CURL en el foro de PHP en Foros del Web. Hola, tengo el siguiente codigo que sirve para crear alertas de google, busque un API anteriormente pero ya no existe, el problema es que al ...
  #1 (permalink)  
Antiguo 21/04/2015, 14:16
 
Fecha de Ingreso: diciembre-2014
Mensajes: 60
Antigüedad: 9 años, 4 meses
Puntos: 1
Acceso a cuenta google con CURL

Hola, tengo el siguiente codigo que sirve para crear alertas de google, busque un API anteriormente pero ya no existe, el problema es que al ejecutar el codigo me sale una pantalla que dice esto...

Verifica que eres tú

Parece que algo cambió en tu modo de acceder a la cuenta. Completa el siguiente paso para garantizarnos que eres tú y no otra persona que se está haciendo pasar por ti. Más información. Y aparecen las opciones de Selecciona un método de verificación:

El codigo es el siguiente...

Código PHP:
Ver original
  1. <?php
  2.  
  3.  
  4.     class Google_alerts_class
  5.     {
  6.        
  7.         public function createAlert( $search ) {
  8.         require('config.php' );
  9.  
  10.             $ch = curl_init();
  11.             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  12.             curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.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, GOOGLE_COOKIEFILE);
  17.             curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_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.             curl_setopt($ch, CURLOPT_URL,
  24.             'https://accounts.google.com/ServiceLogin?hl=es-419&continue=https://www.google.com.mx/%3Fgws_rd%3Dssl');
  25.             $data = curl_exec($ch);
  26.    
  27.             $formFields = $this->getFormFields( $data );
  28.    
  29.             $formFields['Email']  = GOOGLE_USERNAME;
  30.             $formFields['Passwd'] = GOOGLE_PASSWORD;
  31.             unset($formFields['PersistentCookie']);
  32.    
  33.             $post_string = '';
  34.             foreach($formFields as $key => $value) {
  35.                 $post_string .= $key . '=' . urlencode($value) . '&';
  36.             }
  37.    
  38.             $post_string = substr($post_string, 0, -1);
  39.    
  40.             curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
  41.             curl_setopt($ch, CURLOPT_POST, 1);
  42.             curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  43.    
  44.             $result = curl_exec($ch);
  45.    
  46.             if (strpos($result, '<title>Redirecting') === false) {
  47.                 var_dump($result);
  48.                 die("Login failed");
  49.             } else {
  50.  
  51.                 curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/alerts');
  52.                 curl_setopt($ch, CURLOPT_POST, 0);
  53.                 curl_setopt($ch, CURLOPT_POSTFIELDS, null);
  54.    
  55.                 $result = curl_exec($ch);
  56.    
  57.                 // Create alert
  58.    
  59.                 preg_match('/<input type="hidden" name="x" value="([^"]+)"/', $result, $matches);
  60.    
  61.                 $post = array(
  62.                     "x" => $matches[1],         // anti-XSRF key
  63.                     "q" => $search,             // Search term  
  64.                     "t" => ALERT_RESULT_TYPE,   // Result type: 7-everything, 8-discussions
  65.                     "f" => ALERT_FREQUENCY,     // Frequency: 0: as it happens, 1: daily, 6: weekly
  66.                     "l" => ALERT_QUALITY,       // All results: 1, best results: 0
  67.                     "e" => GOOGLE_USERNAME      // Type of delivery: rss: "feed"
  68.                 );
  69.    
  70.                 $post_string = '';
  71.    
  72.                 foreach($post as $key => $value) {
  73.                     $post_string .= $key . '=' . urlencode($value) . '&';
  74.                 }
  75.    
  76.                 $post_string = substr($post_string, 0, -1);
  77.                
  78.                 curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/alerts/create');
  79.                 curl_setopt($ch, CURLOPT_POST, 1);
  80.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  81.    
  82.                 $result = curl_exec($ch);
  83.                 $matches = array();
  84.                 preg_match('#<a href="(https://www.google.com/alerts/feeds/[\d/]+)"#', $result, $matches);
  85.    
  86.                 $top_alert = $matches[1];
  87.    
  88.                 return $top_alert;
  89.             }
  90.         }
  91.    
  92.    
  93.         private function getFormFields($data)
  94.         {
  95.             if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
  96.                 $inputs = $this->getInputs($matches[1]);
  97.    
  98.                 return $inputs;
  99.             } else {
  100.                 die("didn't find login form");
  101.             }
  102.         }
  103.    
  104.         private function getInputs($form)
  105.         {
  106.             $inputs = array();
  107.    
  108.             $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
  109.    
  110.             if ($elements > 0) {
  111.                 for($i = 0; $i < $elements; $i++) {
  112.                     $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
  113.    
  114.                     if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
  115.                         $name  = $name[1];
  116.                         $value = '';
  117.    
  118.                         if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
  119.                             $value = $value[1];
  120.                         }
  121.    
  122.                         $inputs[$name] = $value;
  123.                     }
  124.                 }
  125.             }
  126.    
  127.             return $inputs;
  128.         }
  129.  
  130.     } // end class     
  131.  
  132. ?>

congif.php

Código PHP:
Ver original
  1. <?php
  2.     // --- GOOGLE ACCOUNT SETTINGS -------------------------
  3.     define( 'GOOGLE_USERNAME', '[email protected]' );
  4.     define( 'GOOGLE_PASSWORD', 'theangels');
  5.     // --- ALERT SETTINGS ----------------------------------
  6.     // alert frequency ( how often you recieve emails )
  7.     //  - as it happens: 0
  8.     //  - daily: 1
  9.     //  - weekly: 6
  10.     define( 'ALERT_FREQUENCY', 1 );
  11.     // result type ( what to seaerch )
  12.     //  - everything: 7
  13.     //  - discussions: 8
  14.     //  - news: 1
  15.     //  - blogs: 4
  16.     //  - video: 9
  17.     //  - books: 22
  18.     define( 'ALERT_RESULT_TYPE', 7 );
  19.     // result quality filter
  20.     //  - best results: 0
  21.     //  - all results: 1
  22.     define( 'ALERT_QUALITY', 0 );
  23.     // --- CURL SETTINGS ----------------------------------
  24.     define( 'GOOGLE_COOKIEFILE', 'cookies.txt' );
  25. ?>

create_google_alerts.php
Código PHP:
Ver original
  1. <?php
  2.  
  3.     require('google_alerts_class.php' );
  4.    
  5.     $Ga = new Google_alerts_class();
  6.    
  7.     $query_file_contents = file_get_contents('queries.txt' );
  8.     $queries = explode( "\n", $query_file_contents );
  9.    
  10.     foreach( $queries as $query ) {
  11.         echo "\n  - '$query' Alert Created";
  12.         $Ga->createAlert( $query );
  13.     }
  14.    
  15.     echo "\n\n";
  16.  
  17. ?>
  #2 (permalink)  
Antiguo 21/04/2015, 15:56
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 10 meses
Puntos: 379
Respuesta: Acceso a cuenta google con CURL

y luego? esperas que investiguemos por ti que hacer ?
Para todo lo relacionado con google existe: https://developers.google.com/?hl=es
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.

Etiquetas: curl, google
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:30.