我仍然在新的Google_api_client PHP库中遇到麻烦。我正在尝试检索用户的联系人。
我非常接近正确的解决方案...我的意思是,我刚得到所有结果,但不能解析。
可能是因为我对XML解析器不强。经过测试和测试...我得到了此解决方案(基于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();
}
...
在$ wendesp中,我可以获取我的联系人的全名存储的标题字段,在$ mail_info a中,我获得了一个对象,当我获得电子邮件地址时我会看到地址字段。
这是可悲又丑陋的解决方案...如果我想要公司名称,地址...电话号码...照片。所有这些信息在哪里。
如何在一个很好而干净的解决方案中使用Google响应?
任何人都可以给我一些帮助。再见
有助于我要求JSON而不是XML。尝试在您对Google的请求中将?alt=json
添加到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联系人的PHP示例,我们俩都可能发现了对我们的帮助:
https://code.google.com/p/google-api-php-client/source/browse/browse/trunk/examples/contacts/contacts/simple.php
编辑:
这是另一个可能会有所帮助的链接。在评论中,它描述了使用JSON访问联系人数据的清洁工。
http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-anpi-3-0-and-oauth-2-0-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>";
}