CalendarAPI

De Kafre.org

Os voy contar una cosa que se me ocurrió y que vengo usando para recibir notificaciones en el movil via SMS de forma gratuita.

Resumiendo la cosa es:

  • Google Calendar permite registrar un numero de móvil al que recibir SMS de los eventos que se creen en el Calendar
  • También provee de una API para manejar tus calendarios de google Calendar (por ejemplo thunderbird tiene un plugin con el que las tareas en tu calendario de thunderbird se marcan en el calendario de google).
  • Con estas 2 cosas, un poco de corta y pega se puede hacer un php que se conecte a tu calendario, y te guarde un evento para dentro de un minuto con notificación por SMS, y si tu movil está registrado en google, pues voilá, en menos de un minuto tienes un SMS en tu móvil. Eso si el texto está limitado: Cincuentaitantos caracteres del Título (puede ser mayor pero solo se enviarán esos cincuenta y tantos) y otros tantos del Lugar,

Os adjunto el código para que lo uséis si queréis. Recordad que tenéis que instalaros las librerías de Google Calendar.

  1. #!/usr/bin/php
  2. <?php
  3. /*
  4.  * Created on 23/09/2009
  5.  * By Kafre.org
  6.  * 
  7.  * 
  8.  */
  9.  
  10. require_once 'Zend/Loader.php';
  11. Zend_Loader::loadClass('Zend_Gdata');
  12. Zend_Loader::loadClass('Zend_Gdata_AuthSub');
  13. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  14. Zend_Loader::loadClass('Zend_Gdata_Calendar');
  15.  
  16. class Zend_Gdata_CalendarTyven {
  17.  
  18.     public function __construct($user,$pass,$URL)    {
  19.         $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
  20.         $this->client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  21.         $this->gdata = new Zend_Gdata_Calendar($this->client);
  22.         $this->URL=$URL;
  23.     }
  24.  
  25.  
  26.     public function createEvent (
  27.             $title = '57 characters', $desc='Meet for a quick lesson', 
  28.             $where = '52 characters', $startDate = '2008-01-20', $startTime = '10:00',
  29.             $endDate = '2008-01-20', $endTime = '11:00', $tzOffset = '+01',
  30.             $method = 'sms', $minutes = '00') {
  31.  
  32.         $CalendarInstance = $this->gdata; 
  33.         $newEntry = $CalendarInstance->newEventEntry();
  34.         $newEntry->title = $CalendarInstance->newTitle($title);
  35.                 $newEntry->where = array($CalendarInstance->newWhere($where));
  36.                 $newEntry->content = $CalendarInstance->newContent("$desc");
  37.  
  38.                 $when = $CalendarInstance->newWhen();
  39.                 $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}";
  40.                 $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}";
  41.                 $reminder = $CalendarInstance->newReminder();
  42.         $reminder->minutes = $minutes;
  43.         $reminder->method = $method;
  44.         $when->reminders = array($reminder);
  45.         $newEntry->when = array($when);
  46.  
  47.  
  48.         $createdEvent=$CalendarInstance->insertEvent($newEntry,"http://www.google.com/calendar/feeds/" . $this->URL . "/private/full");
  49.         return $createdEntry->id->text;
  50.     }
  51.  
  52.  
  53.         function outputCalendar() {
  54.                 $gdataCal = $this->gdata;
  55.                 print
  56.                 $calendarFeed = $gdataCal->getCalendarListFeed();
  57.                 echo "<ul>\n";
  58.                 foreach ($calendarFeed as $calendar) {
  59.                         echo "\t<li>" . $calendar->title->text .  " (" . $calendar->id->text . ")\n";
  60.                         echo "\t</li>\n";
  61.                 }
  62.                 echo "</ul>\n";
  63.         }
  64. }
  65.  
  66.  
  67.  
  68.  
  69. /**
  70.  * @Get atomic time
  71.  * @throws Exception of failure
  72.  * @return int
  73.  */
  74. function atomicTime()
  75. {
  76.     /*** connect to the atomic clock ***/
  77.     $fp = @fsockopen( "time-a.nist.gov", 37, $errno, $errstr, 10 );
  78.     if ( !$fp )
  79.     {
  80.         throw new Exception( "$errno: $errstr" );
  81.     }
  82.     else
  83.     { 
  84.         fputs($fp, "\n"); 
  85.         $time_info = fread($fp, 49);
  86.         fclose($fp);
  87.     }
  88.     /*** create the timestamp ***/
  89.     $atomic_time = (abs(hexdec('7fffffff') - hexdec(bin2hex($time_info)) - hexdec('7fffffff')) - 2208988800); 
  90.     echo $errstr;
  91.     return $atomic_time;
  92. }
  93.  
  94. try
  95.     {
  96.         /*** get the timestamp ***/
  97.         $ts1 = atomicTime();
  98.  
  99.     }
  100.     catch(Exception $e)
  101.     {
  102.         echo $e->getMessage();
  103.     }
  104.  
  105. $Texto1=$argv[1];
  106. $Texto2=$argv[2];
  107. $Texto3=$argv[3];
  108.  
  109.  
  110. $Calendario= new Zend_Gdata_CalendarTyven('myaccount@mydomain.com', 'mypassword','myCalendarURLFromTheConfigurationk%40group.calendar.google.com');
  111. #print_r($Calendario->client);
  112. #print_r($Calendario->fecha);
  113.  
  114. //$ts1=strtotime($Calendario->fecha);
  115.  
  116. $Zona=date('P');
  117. $ts1=$ts1 + 1*60;
  118. $ts2=$ts1 + 2*60;
  119.  
  120. $fecha=date('Y-m-d',$ts1);
  121. $fecha2=date('Y-m-d',$ts2);
  122. $hora=date('G:i',$ts1);
  123. $hora2=date('G:i',$ts2);
  124.  
  125. $ID=$Calendario->createEvent($Texto1, $Texto3, $Texto2, $fecha, $hora, $fecha2, $hora2, $Zona);
  126. ?>
Herramientas personales