使用 PHP Google-API-php-client-0.6.0 获取 Gmail 联系人



我正在使用Google-API-php-client-0.6.0实现Gmail联系人获取

if ($client->getAccessToken()) {
  $req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full?max-results=9999&alt=json");
  $val = $client->getIo()->authenticatedRequest($req);
  // The contacts api only returns XML responses.
  //$response = json_encode(simplexml_load_string($val->getResponseBody()));
   print_r(json_decode($val->getResponseBody())) ;
  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();
}

使用此 HTTprequest iam 获取用户详细信息结果,如下所示

[0] => stdClass Object
                    (
                        [id] => stdClass Object
                            (
                                [$t] => http://www.google.com/m8/feeds/contacts/sivagopaltech%40gmail.com/base/1301bd0ce878b5
                            )
                        [updated] => stdClass Object
                            (
                                [$t] => 2011-10-10T04:40:56.311Z
                            )
                        [category] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [scheme] => http://schemas.google.com/g/2005#kind
                                        [term] => http://schemas.google.com/contact/2008#contact
                                    )
                            )
                        [title] => stdClass Object
                            (
                                [type] => text
                                [$t] => 
                            )
                        [link] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo
                                        [type] => image/*
                                        [href] => https://www.google.com/m8/feeds/photos/media/sivagopaltech%40gmail.com/1301bd0ce878b5/1B2M2Y8AsgTpgAmY7PhCfg
                                    )
                                [1] => stdClass Object
                                    (
                                        [rel] => self
                                        [type] => application/atom+xml
                                        [href] => https://www.google.com/m8/feeds/contacts/sivagopaltech%40gmail.com/full/1301bd0ce878b5
                                    )
                                [2] => stdClass Object
                                    (
                                        [rel] => edit
                                        [type] => application/atom+xml
                                        [href] => https://www.google.com/m8/feeds/contacts/sivagopaltech%40gmail.com/full/1301bd0ce878b5/1318221656311001
                                    )
                            )
                        [gd$email] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [rel] => http://schemas.google.com/g/2005#other
                                        [address] => jagadeesh.miriyala@gmail.com
                                        [primary] => true
                                    )
                            )
                    )

在我看来,$t和其他人出乎意料的地方,在这种情况下,我正在获取用户电子邮件ID

当我更改代码和httpRequest时,如下所示,我只获得用户名而不是电子邮件ID

if ($client->getAccessToken()) {
  $req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full?max-results=9999");
  $val = $client->getIo()->authenticatedRequest($req);
  // The contacts api only returns XML responses.
  $response = json_encode(simplexml_load_string($val->getResponseBody()));
   print_r(json_decode($response)) ;
  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();
}

用户详细信息格式是这样的

[title] => 96 76 36 78 07 chakri        // username
                    [link] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [@attributes] => stdClass Object
                                        (
                                            [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo
                                            [type] => image/*
                                            [href] => https://www.google.com/m8/feeds/photos/media/sivagopaltech%40gmail.com/826b6c0fa89181/a9haUsi43SXQrgy78Gjg4Q
                                        )
                                )
                            [1] => stdClass Object
                                (
                                    [@attributes] => stdClass Object
                                        (
                                            [rel] => http://schemas.google.com/contacts/2008/rel#photo
                                            [type] => image/*
                                            [href] => https://www.google.com/m8/feeds/photos/media/sivagopaltech%40gmail.com/826b6c0fa89181
                                        )
                                )
                            [2] => stdClass Object
                                (
                                    [@attributes] => stdClass Object
                                        (
                                            [rel] => self
                                            [type] => application/atom+xml
                                            [href] => https://www.google.com/m8/feeds/contacts/sivagopaltech%40gmail.com/full/826b6c0fa89181
                                        )
                                )
                            [3] => stdClass Object
                                (
                                    [@attributes] => stdClass Object
                                        (
                                            [rel] => edit
                                            [type] => application/atom+xml
                                            [href] => https://www.google.com/m8/feeds/contacts/sivagopaltech%40gmail.com/full/826b6c0fa89181/1349307874947993
                                        )
                                )
                        )
                )

任何人都可以建议我如何避免这些$t变量或如何使用第二个HTTP请求获取电子邮件ID

问题是 simplexml 如何格式化响应。 它会删除电子邮件字段。 将"?alt=json"添加到 API 调用的末尾,它将返回一个 JSON。 json_decode响应,您将拥有一个带有电子邮件的漂亮 PHP 对象

相关内容

  • 没有找到相关文章

最新更新