tpae
tpae

Reputation: 6346

Google Contacts API XML parsing with PHP

I have an xml feed from Google Contacts API, but I have trouble reading <gd: tags.

$xml = simplexml_load_string($google_contacts);

This reads every elements except the <gd: tags, which contains e-mail information.

What's the best thing to do?

Upvotes: 2

Views: 3639

Answers (4)

Mikhail Levkovsky
Mikhail Levkovsky

Reputation: 86

something that I did to make it easer to work with is I got the gdata back as json by appending alt=json to the request string

My full query string looks like:

https://www.google.com/m8/feeds/contacts/{userEmail}/full?oauth_token=token&max-results=9999999&alt=json');

Then it's just regular json.

Upvotes: 2

Hamid
Hamid

Reputation: 121

I had the same problem which could not access to <gd: nodes. I make my own solution which I need the fUll list of contacts info under each entry. I guess this is because of colon ":" in each <gd: tag then replace it with no ":" here is what I did:

$search=array('<gd:','</gd:');
$replace=array('<gd','</gd');
/*Create json object from $google_contacts(atom xml string)
by replacing "gd:" to "gd"*/
$response = json_encode(str_replace($search, $replace,$google_contacts));
/*Create Array from $response*/
$contactsList = json_decode($response, true);
print "<pre>" . print_r($contactsList['entry'], true);

this will make the following Google Contact xml:

<? xml version = '1.0' encoding = 'UTF-8' ?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'>

    ...

    <entry>
        <id>http://www.google.com/m8/feeds/contacts/xxxxxxxx%40gmail.com/base/0</id>
        <updated>2012-09-23T01:54:56.646Z</updated>
        <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
        <title type='text'>Will Jordan</title>
        <link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0/eAEUjCL-B1cSXN'/>
        <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0'/>
        <link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0'/>
        <link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0/1348365xxxxxx'/>
        <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/>
        <gd:email rel='http://schemas.google.com/g/2005#home' address='[email protected]'/>
        <gd:phoneNumber rel='http://schemas.google.com/g/2005#home'>8888888888</gd:phoneNumber>
        <gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile'>9999999999</gd:phoneNumber>
        <gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>5555555555</gd:phoneNumber>
        <gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/xxxxxxx%40gmail.com/base/4fde39d984'/>
        <gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/xxxxxxx%40gmail.com/base/3f5dc5d3be0'/>
    </entry>
</feed>

to this print_r output of array:

array(
[id] => http://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/base/0
  [updated] => 2013-08-27T19:33:22.431Z
  [category] => Array(
      [@attributes] => Array(
          [scheme] => http://schemas.google.com/g/2005#kind
          [term] => http://schemas.google.com/contact/2008#contact
        )
    )
  [title] => Will Jordan
  [link] => Array(
      [0] => Array(
          [@attributes] => Array(
              [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo
              [type] => image/ *
              [href] => https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0/eAEUjCL-B1cSX
            )
        )
      [1] => Array(
          [@attributes] => Array(
              [rel] => http://schemas.google.com/contacts/2008/rel#photo
              [type] => image/ *
              [href] => https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0
            )
        )
      [2] => Array(
          [@attributes] => Array(
              [rel] => self
              [type] => application/atom+xml
              [href] => https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0
            )
        )
      [3] => Array(
          [@attributes] => Array(
              [rel] => edit
              [type] => application/atom+xml
              [href] => https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0/1377632
            )
        )
    )
  [gdemail] => Array(
      [0] => Array(
          [@attributes] => Array(
              [rel] => http://schemas.google.com/g/2005#other
              [address] => [email protected]
              [primary] => true
            )
        )
      [1] => Array(
          [@attributes] => Array(
              [rel] => http://schemas.google.com/g/2005#home
              [address] => [email protected]
            )
        )
    )
  [gdphoneNumber] => Array(
      [0] => 8888888888
      [1] => 9999999999
      [2] => 5555555555
    )
)

Upvotes: 0

Sanjeev Chauhan
Sanjeev Chauhan

Reputation: 4097

You can read "gd:" tags using xml_parse. It return all result in array, as xml string is not shared by you so you need to write further logic to get data from array

<?php
$content = $google_contacts;
$parser = xml_parser_create();
xml_parse_into_struct($parser, $content, $data, $index);
xml_parser_free($parser);
print"<pre>";
print_r($data);
/*foreach($data as $vals )
{    
  //write your code to get result from array
}*/
?>

http://www.php.net/manual/en/function.xml-parse.php

Upvotes: 1

hakre
hakre

Reputation: 197682

Update: If you're using name-spaces and xpath, you might need to register them first in SimpleXML.

If you have problems to access an element, try to enclose it within {} angel brackets, this probably helps: $xml->{gd:...}.


SimpleXML has no/limited support for XML-Namespaces if I remember correctly. DomDocument has, so take it instead.

For a first start:

$dom = new DomDocument;
$dom->loadXML($google_contacts);
...

Or use DomDocument for the part that is difficult to do in SimpleXML only:

$domElement = dom_import_simplexml($simpleXmlNode);

Upvotes: 2

Related Questions