2009-08-28 15 views
6

Recibo XML del calendario público de google. Se ve así:Análisis del feed XML del calendario de google

<?xml version='1.0' encoding='UTF-8'?> 
    <feed xmlns='................' xmlns:gd='http://schemas.google.com/g/2005'> 
     <id>http://www.google.com/calendar/feeds/........./public/full</id> 
     <updated>2009-08-24T10:57:00.000Z</updated> 
     <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/> 
      <title type='text'>Sports Events</title> 
    ..... 
     <entry> 
      <id>...........</id> 
      <published>2009-08-14T00:29:58.000Z</published> 
      <updated>2009-08-14T00:29:58.000Z</updated> 
      <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/> 
.............. 
      <georss:where> 
       <gml:Point> 
        <gml:pos>11.111111 -22.22222</gml:pos> 
       </gml:Point> 
      </georss:where> 
      <gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'/> 
      <gd:transparency value='http://schemas.google.com/g/2005#event.transparent'/> 
      <gCal:uid value='[email protected]'/> 
      <gCal:sequence value='0'/> 
      <gd:when startTime='2009-10-03' endTime='2009-10-04'/> 

............. 

ahora a PHP:

$calendar = simplexml_load_file('http://my.feed.com'); 
foreach ($calendar->entry as $item) { 
    //here I get the whole <entry> node 
    $gd = $item->children('http://schemas.google.com/g/2005'); 
    // now in $gd I have all nodes like <gd:XXXXX> 
} 

El problema es cómo obtener valor a partir de aquí?

<gml:pos>11.111111 -22.22222</gml:pos> 

No está presente en mi variable $ gd, ¿cómo obtener este nodo?

Respuesta

10

Recomiendo usar esta biblioteca: https://github.com/collegeman/coreylib. Hará todo de principio a fin de una forma increíblemente fácil.

+1

Oh, esto es simplemente genial. Gracias por mostrarme esta biblioteca :) – 6bytes

+0

Haha my pleasure! es genial, ¿no? –

+0

Eso es muy impresionante –

1

Puede usar la biblioteca Zend GData para analizar los servicios web de Google (incluido el calendario). Es más fácil que tratar de usar el código XML manualmente. Hay un tutorial que le muestra cómo hacer esto here.

+0

Gracias pero coreylib funcionó como magia :) – 6bytes

+0

¿Tienes un nuevo enlace? – Zoredache

5

Por supuesto, hay varias formas de analizar el XML. Display Google Calendar events on your PHP Web site with XPath (consulte "Análisis del feed de Google Calendar con PHP") y Integrate your PHP application with Google Calendar son dos recursos completos con código de muestra y más.

Personalmente, he utilizado el siguiente enfoque:

$doc = new DOMDocument(); 
$doc->load('http://my.feed.com'); 
$entries = $doc->getElementsByTagName("entry"); 
foreach ($entries as $entry) { 
    $titles = $entry->getElementsByTagName("title"); 
    $title = $titles->item(0)->nodeValue; 

    $times = $entry->getElementsByTagName("when"); 
    $startTime = $times->item(0)->getAttributeNode("startTime")->value; 
    $when = date("l jS \o\f F Y - h:i A", strtotime($startTime)); 
    // ... 
} 

Con el fin de acceder al espacio de nombres georss etc. echar un vistazo a (y su salida)

foreach ($doc->getElementsByTagNameNS('*', '*') as $element) { 
    echo 'localName: ', $element->localName, ', prefix: ', $element->prefix, "\n"; 
} 
0

Calendario completo era mucho más fácil que Coreylib. Coreylib no fue tan simple, aunque estoy seguro de que es muy poderoso.

Tenía FullCalendar ejecutándose en 5 minutos. http://arshaw.com/fullcalendar/docs/google_calendar/

+2

Esta pregunta está etiquetada php, por lo que su solución jQuery está fuera de línea y es engañosa si ni siquiera está diciendo que es una alternativa de jQuery. – wdyp

Cuestiones relacionadas