【问题标题】:PHP Google API Client v3 get contactsPHP Google API Client v3 获取联系人
【发布时间】:2012-11-04 17:54:58
【问题描述】:

我仍然无法使用新的 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 响应?

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

【问题讨论】:

    标签: php xml-parsing google-api google-api-php-client


    【解决方案1】:

    帮助我的是请求 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 example,我们可能都发现它对我们有帮助:

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

    编辑:

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

    【讨论】:

    • 如果您使用 file_get_contents 而不是 curl_file_get_contents,效果会很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多