jac*_*lli 1 php google-api xml-parsing google-api-php-client
我仍然遇到新的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();
}
...
Run Code Online (Sandbox Code Playgroud)
在$ response中,我可以获得存储联系人姓名的title字段,在$ mail_info中有一个对象,当我收到电子邮件地址时,我会看到地址字段.
这是SAD和UGLY解决方案......如果我想要公司名称,地址......电话号码......照片怎么办?所有这些信息在哪里?
如何在一个优秀而干净的解决方案中使用Google响应?
任何人都可以给我一些帮助.再见
帮助我的是请求JSON而不是XML.尝试?alt=json在您对谷歌发出的请求中添加到网址末尾.
$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);
Run Code Online (Sandbox Code Playgroud)
当然不是孩子的游戏来获得你想要的东西但是使用php数组可能更容易.
为了完整性,这是谷歌联系人php示例,我们都可能发现它帮助了我们:
https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/contacts/simple.php
编辑:
这是另一个可能有用的链接.在评论中,它描述了使用JSON访问联系人数据的清洁工.
$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>";
}
Run Code Online (Sandbox Code Playgroud)
和
$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>";
}
Run Code Online (Sandbox Code Playgroud)