PHP Google API Client v3获取联系人

I still have trouble with the new google_api_client php library. I'm trying to retrieve the user's contacts.

I'm very close to the right solution ... I mean, I just got all the results but a can't parse it.

Probably it's because I'm not strong with XML parser. After tests and tests ... I get this solution (based on the example file by Google):

...
$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full");         
$val = $client->getIo()->authenticatedRequest($req);
$response = simplexml_load_string($val->getResponseBody());

foreach($response->entry as $entry)
{
    $child = $entry->children("http://schemas.google.com/g/2005");
    $mail_info = $child->attributes();
}
...

In the $response I can get the title field where my contact's full name is stored, and in the $mail_info a got an object where i see the address field when I get the email address.

It's SAD and UGLY solution ... what if I want the company name, address ... phone numbers ... photos. Where are all these informations.

How can I use the Google response in a great and clean solution?

Anyone can give me some help. Bye

What helped me was requesting JSON instead of XML. Try adding ?alt=json to the end of the URL in the request you make to google.

$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full?alt=json");         
$val = $client->getIo()->authenticatedRequest($req);
$string = $val->getResponseBody();
$phparray = json_decode($string);

Certainly not child's play to get what you want but working with php arrays is probably easier.

For completeness this is the google contacts php example that we both probably found that helped us:

https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/contacts/simple.php

EDIT:

Here is another link that might help. In the comments it describes a cleaner of accessing contact's data using JSON.

http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0-in-php/

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse =  curl_file_get_contents($url);

$temp = json_decode($xmlresponse,true);

foreach($temp['feed']['entry'] as $cnt) {
    echo $cnt['title']['$t'] . " --- " . $cnt['gd$email']['0']['address'] . "</br>";
}

and

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse =  curl_file_get_contents($url);

$temp = json_decode($xmlresponse,true);

foreach($temp['feed']['entry'] as $cnt) {
    echo $cnt['title']['$t'] . " --- " . $cnt['gd$email']['0']['address'];
    if(isset($cnt['gd$phoneNumber'])) echo " --- " . $cnt['gd$phoneNumber'][0]['$t'];
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$street'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$street']['$t'];
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$neighborhood'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$neighborhood']['$t'];
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$pobox'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$pobox']['$t'];
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$postcode'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$postcode']['$t'];
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$city'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$city']['$t'];
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$region'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$region']['$t'];
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$country'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$country']['$t'];
    echo "</br>";
}