1

我仍然无法使用新的 google_api_client php 库。我正在尝试检索用户的联系人。

我非常接近正确的解决方案......我的意思是,我刚刚得到了所有结果,但无法解析它。

可能是因为我不擅长 XML 解析器。经过测试和测试......我得到了这个解决方案(基于谷歌的示例文件):

...
$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();
}
...

在 $response 中,我可以获得存储联系人全名的标题字段,在 $mail_info 中,我获得了一个对象,当我获得电子邮件地址时,我可以在其中看到地址字段。

这是一个悲伤和丑陋的解决方案......如果我想要公司名称,地址......电话号码......照片怎么办。所有这些信息在哪里。

如何在出色而干净的解决方案中使用 Google 响应?

任何人都可以给我一些帮助。再见

4

1 回答 1

6

帮助我的是请求 JSON 而不是 XML。尝试?alt=json在您向 google 发出的请求中添加到 URL 的末尾。

$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);

获得你想要的东西当然不是儿戏,但使用 php 数组可能更容易。

为了完整起见,这是我们可能都发现对我们有帮助的google contacts php 示例:

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

编辑:

这是另一个可能有帮助的链接。在评论中,它描述了使用 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>";
}

$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>";
}
于 2013-11-07T22:25:52.690 回答