使用 JSON 列出表中的所有联系人

发布于 2024-12-13 23:49:09 字数 14661 浏览 0 评论 0原文

我目前正在使用此功能从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始 2024-12-20 23:49:09

返回的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 对象/数组。当然,在这种情况下,您需要构建一些检查,在尝试获取值之前仔细检查字段是否存在。否则,您会收到警告或错误。

Is the JSON that is returned valid? You could paste the result into JSONLint and see if it returns errors.

json_decode() should return NULL for invalid input. Try using var_dump(json_decode($response). Does it return NULL or some other value?

Also, you should be aware that json_decode() returns an object by default, so doing something like echo $response['somekey']; wouldn't work. To get an associative array, use json_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:

<?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";
}

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.

若水般的淡然安静女子 2024-12-20 23:49:09

是的,运行 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() with var_dump() and you will se the whole object with it's childs.

Regards
Tobias

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文