使用 JSON 列出表中的所有联系人
我目前正在使用此功能从 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] => [email protected]
)
[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] => [email protected]
)
)
)
[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
)
)
)
I'm currently using this function to get all my contacts from Google Contacts.
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);
To print the whole JSON works fine with echo $respone;
but I don't know how to list all of the contacts (full name, phone and image/avatar) in a table. I have tried json_decode()
and even Google'd for a solution for a few days now with no result.
I'm asking you know - how can I fix this?
Thanks in advance.
EDIT
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] => [email protected]
)
[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] => [email protected]
)
)
)
[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
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
返回的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"]。
如果我解释的话,应该输出一个表格。正确的示例:
请注意,您不能在键周围使用双引号(例如 $entry["gd$name"]),除非您像这样转义它们: $entry["gd\$name"] 如果您使用单引号,
如果 API 并不总是返回所有字段(例如,如果您没有填写用户的生日,则 API 可能会返回空字段,也可能不会返回) 。返回
gContact$birthday
对象/数组。当然,在这种情况下,您需要构建一些检查,在尝试获取值之前仔细检查字段是否存在。否则,您会收到警告或错误。Is the JSON that is returned valid? You could paste the result into JSONLint and see if it returns errors.
json_decode()
should returnNULL
for invalid input. Try usingvar_dump(json_decode($response)
. Does it returnNULL
or some other value?Also, you should be aware that
json_decode()
returns an object by default, so doing something likeecho $response['somekey'];
wouldn't work. To get an associative array, usejson_decode($response, true);
.Edit: The
foreach
was just an example. At the time, you didn't post any output, so I had to guess what would be returned.If you decode the JSON response into an array, you can iterate over the results using
foreach
, from your sample, it seems that the contacts are in$the_array["feed"]["entries"]
.A crude example that should to output a table, if I interpreted the sample correctly:
Note that you can't use double quotes around the keys (such as $entry["gd$name"]) unless you escape them like so: $entry["gd\$name"]. If you use single quotes, you won't have to do that.
If the API doesn't always return all fields (for example, if you haven't filled in a birthdate for the user, the API might return an empty field or it might not return the
gContact$birthday
object/ array at all. In that case, you'll need to build in some checks that double-check that a field exists before trying to get the value, of course. Otherwise, you'd get warnings or errors.是的,运行
json_decode()
后,您将获得一个包含所有联系人列表的对象。现在您需要循环遍历所有联系人(例如使用 foreach 循环)如果您不知道对象中联系人的确切位置,只需使用
打印
,您将看到整个对象及其子对象。json_decode()
的结果var_dump()问候
托比亚斯
Yes, after you run
json_decode()
you get an object with a list of all contacts. Now you need to loop through all the contacts (e.g. using a foreach loop)If you don't know the exact location of the contacts in you object just print the result of
json_decode()
withvar_dump()
and you will se the whole object with it's childs.Regards
Tobias