我目前正在使用此功能从Google通讯录中获取所有联系人。
session_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/accounts/ClientLogin');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = array(
'accountType' => 'GOOGLE',
'Email' => 'email',
'Passwd' => 'password',
'source'=>'sourcetest',
'service'=>'cp'
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$responses = explode("\n", curl_exec($ch));
$_SESSION['auth'] = str_replace('Auth=', '', $responses[2]);
$_SESSION['email'] = 'email';
$url = 'https://www.google.com/m8/feeds/contacts/default/full';
$url .= '?group=http://www.google.com/m8/feeds/groups/'.$_SESSION['email'].'/base/6';
$url .= '&max-results=500&alt=json';
$ch = curl_init($url);
$header[] = 'Authorization: GoogleLogin auth='.$_SESSION['auth'];
$header[] = 'GData-Version: 3.0';
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
curl_close($ch);
要打印整个JSON可以正常使用echo $respone;
,但我不知道如何在表格中列出所有联系人(全名,电话和图像/头像)。我已经尝试json_decode()
,甚至Google已经尝试了几天的解决方案而没有结果。
我问你 - 我怎么解决这个问题?
提前致谢。
修改
Array
(
[version] => 1.0
[encoding] => UTF-8
[feed] => Array
(
[xmlns] => http://www.w3.org/2005/Atom
[xmlns$openSearch] => http://a9.com/-/spec/opensearch/1.1/
[xmlns$gContact] => http://schemas.google.com/contact/2008
[xmlns$batch] => http://schemas.google.com/gdata/batch
[xmlns$gd] => http://schemas.google.com/g/2005
[gd$etag] => W/"{code}"
[id] => Array
(
[$t] => mymail@gmail.com
)
[updated] => Array
(
[$t] => 2011-11-07T13:50:54.073Z
)
[category] => Array
(
[0] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://schemas.google.com/contact/2008#contact
)
)
[title] => Array
(
[$t] => edgren's Contacts
)
[link] => Array
(
[0] => Array
(
[rel] => alternate
[type] => text/html
[href] => http://www.google.com/
)
[1] => Array
(
[rel] => http://schemas.google.com/g/2005#feed
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full
)
[2] => Array
(
[rel] => http://schemas.google.com/g/2005#post
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full
)
[3] => Array
(
[rel] => http://schemas.google.com/g/2005#batch
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full/batch
)
[4] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full?alt=json&max-results=500&group=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2Fgroups%2Fmymail%40gmail.com%2Fbase%2F6
)
)
[author] => Array
(
[0] => Array
(
[name] => Array
(
[$t] => edgren
)
[email] => Array
(
[$t] => mymail@gmail.com
)
)
)
[generator] => Array
(
[version] => 1.0
[uri] => http://www.google.com/m8/feeds
[$t] => Contacts
)
[openSearch$totalResults] => Array
(
[$t] => 73
)
[openSearch$startIndex] => Array
(
[$t] => 1
)
[openSearch$itemsPerPage] => Array
(
[$t] => 500
)
[entry] => Array
(
[0] => Array
(
[gd$etag] => "{code}"
[id] => Array
(
[$t] => http://www.google.com/m8/feeds/contacts/mymail%40gmail.com/base/{code}
)
[updated] => Array
(
[$t] => 2011-09-12T17:28:57.835Z
)
[app$edited] => Array
(
[xmlns$app] => http://www.w3.org/2007/app
[$t] => 2011-09-12T17:28:57.835Z
)
[category] => Array
(
[0] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://schemas.google.com/contact/2008#contact
)
)
[title] => Array
(
[$t] => {contacts name}
)
[link] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/contacts/2008/rel#photo
[type] => image/*
[href] => https://www.google.com/m8/feeds/photos/media/mymail%40gmail.com/{code}
[gd$etag] => "{code}"
)
[1] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full/{code}
)
[2] => Array
(
[rel] => edit
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full/{code}
)
)
[gd$name] => Array
(
[gd$fullName] => Array
(
[$t] => {contacts name}
)
[gd$givenName] => Array
(
[$t] => {contacts last name}
)
[gd$familyName] => Array
(
[$t] => {contacts first name}
)
)
[gContact$birthday] => Array
(
[when] => {contacts birthday}
)
[gd$email] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/g/2005#other
[address] => {contacts email}
[primary] => true
)
)
[gd$phoneNumber] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/g/2005#mobile
[$t] => {contacts number}
)
)
[gd$structuredPostalAddress] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/g/2005#home
[gd$formattedAddress] => Array
(
[$t] => {contacts address}
)
[gd$street] => Array
(
[$t] => {contacts address}
)
[gd$postcode] => Array
(
[$t] => {contacts address}
)
[gd$city] => Array
(
[$t] => {contacts address}
)
[gd$region] => Array
(
[$t] => {contacts address}
)
[gd$country] => Array
(
[code] => SE
[$t] => Sverige
)
)
)
[gContact$website] => Array
(
[0] => Array
(
[href] => {contacts website}
[rel] => profile
)
)
[gd$extendedProperty] => Array
(
[0] => Array
(
[name] => workAddrTB
[value] => WorkAddress2=/WorkCity=/WorkState=/WorkZipCode=/WorkCountry=
)
[1] => Array
(
[name] => homeAddrTB
[value] => HomeAddress2=/HomeCity=/HomeState=/HomeCountry=Sverige/HomeZipCode=
)
[2] => Array
(
[name] => contactTB
[value] => PreferMailFormat=0/allowRemoteContent=
)
)
[gContact$groupMembershipInfo] => Array
(
[0] => Array
(
[deleted] => false
[href] => http://www.google.com/m8/feeds/groups/mymail%40gmail.com/base/6
)
[1] => Array
(
[deleted] => false
[href] => http://www.google.com/m8/feeds/groups/mymail%40gmail.com/base/d
)
)
)
答案 0 :(得分:1)
返回的JSON是否有效?您可以将结果粘贴到JSONLint中,看看它是否返回错误。
json_decode()
应返回NULL
以获取无效输入。尝试使用var_dump(json_decode($response)
。它会返回NULL
还是其他一些值?
此外,您应该知道json_decode()
默认返回一个对象,因此执行echo $response['somekey'];
之类的操作无效。要获取关联数组,请使用json_decode($response, true);
。
编辑:foreach
只是一个例子。当时,你没有发布任何输出,所以我不得不猜测会返回什么。
如果您将JSON响应解码为数组,则可以使用foreach
从您的示例中迭代结果,似乎联系人位于$the_array["feed"]["entries"]
。
如果我正确解释了样本,应该输出表格的粗略示例:
<?php
$response_as_array = json_decode($response, true);
if(isset($response_as_array['feed']) and isset($response_as_array['feed']['entry']))
{
echo '<table border="1">'. "\n";
foreach($response_as_array['feed']['entries'] as $i = > $entry)
{
$info = array(
'title' => $entry['title'],
'fullName' => $entry['gd$name']['gd$fullName']['$t'],
'givenName' => $entry['gd$name']['gd$givenName']['$t'],
'familyName' => $entry['gd$name']['gd$familyName']['$t'],
'birthday' => $entry['gContact$birthday']['when'],
'email' => (isset($entry['gd$email'][0])
? $entry['gd$email'][0]['address'] : ''),
'phoneNumber' => (isset($entry['gd$phoneNumber'][0])
? $entry['gd$phoneNumber'][0][$t] : ''),
// and so on
);
// If this is the first time this loop is run,
// then we want to output the column names.
if($i === 0) {
echo '<tr>'. "\n";
foreach(array_keys($info) as $column_name) {
echo '<th>'. $column_name .'</th>';
}
echo '</tr>' . "\n";
}
echo '<tr>'. "\n";
foreach($info as $value) {
echo '<td>'. $value .'</td>'. "\n";
}
echo '</tr>'. "\n";
}
echo '</table>'. "\n";
}
请注意,您不能在键周围使用双引号(例如$ entry [“gd $ name”]),除非您像这样对它们进行转义:$ entry [“gd \ $ name”]。如果您使用单引号,则不必这样做。
如果API并不总是返回所有字段(例如,如果您没有为用户填写生日,API可能会返回一个空字段,或者它可能不会返回gContact$birthday
个对象/在这种情况下,当然,你需要在尝试获取值之前构建一些检查字段是否存在的检查。否则,你会收到警告或错误。
答案 1 :(得分:0)
是的,运行json_decode()
后,您会收到一个包含所有联系人列表的对象。现在你需要遍历所有联系人(例如使用foreach循环)
如果您不知道对象中联系人的确切位置,只需使用json_decode()
打印var_dump()
的结果,您就会将整个对象与其子项一起显示。
问候
托拜厄斯