【问题标题】:google+ oAuth contacts full list in PHPPHP 中的 google+ oAuth 联系人完整列表
【发布时间】:2012-07-12 20:11:40
【问题描述】:

我正在使用谷歌最新的 api 来检索联系人列表。此代码似乎以 XML 格式输出我列表的前 20 项。
问题:如何输出整个列表?

<?php
require_once '../../src/apiClient.php';
session_start();

$client = new apiClient();
$client->setApplicationName('Google Contacts PHP Sample');
$client->setScopes("http://www.google.com/m8/feeds/");
// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html
// Visit https://code.google.com/apis/console?api=contacts to generate your
// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');

if (isset($_GET['code'])) {
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
    unset($_SESSION['token']);
    $client->revokeToken();
}

if ($client->getAccessToken()) {
    $req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
    $val = $client->getIo()->authenticatedRequest($req);

    // The contacts api only returns XML responses.
    /* * * * * * * * * produced an error
    $response = json_encode(simplexml_load_string($val->getResponseBody()));
    print "<pre>" . print_r(json_decode($response, true), true) . "</pre>"; 
    */

    // fix
    $response = $val->getResponseBody();
    print "<pre>" . print_r($response) . "</pre>";


    // The access token may have been updated lazily.
    $_SESSION['token'] = $client->getAccessToken();
} else {
    $auth = $client->createAuthUrl();
}

if (isset($auth)) {
        print "<a class=login href='$auth'>Connect Me!</a>";
    } else {
        print "<a class=logout href='?logout'>Logout</a>";
}
?>

我看到了这个response,但不知道如何实现它

首先,您需要询问版本 3,因为 extendedProperties 是 没有返回。这是一些工作代码的 c-n-p。

function get($xmlfile) {
try {
  @$feed = simplexml_load_file($xmlfile. '&v=3.0');
  if ($feed === FALSE) {
    throw new Exception(file_get_contents($xmlfile));
  }
} catch (Exception $e) {
  return array('error' => true, 'payload' => $e->getMessage());
}
return array('error' => false, 'payload' => $xmlData);
}

接下来要寻找的是告诉 simplexml 你想要的东西 使用 gd 命名空间。另一个例子:

 $this->nsGd = $xmlData->children('http://schemas.google.com/g/2005');
 ...
 $this->email = @(string)$this->nsGd->email->attributes()->address;
 ...
 foreach ($this->nsGd->extendedProperty as $x) {
    if ($x->attributes()->name == 'ethnicity') {
      $this->ethnicity = $x->attributes()->value;
    }
  } 

【问题讨论】:

    标签: php oauth google-plus


    【解决方案1】:

    您是否尝试过使用 max-results 查询参数?来自Contacts API documentation

    注意:Feed 可能不包含用户的所有联系人,因为返回的结果数量存在默认限制。更多 信息见Retrieving contacts using query parameters中的max-results查询参数。

    因此,您可以发送如下请求:https://www.google.com/m8/feeds/contacts/default/full?max-results=1000

    【讨论】:

    • 我觉得答案相互矛盾,因为上面写着To retrieve all of a user's contacts, send an authorized GET request to the following URL:,但后来又说可能有限制
    • 请求默认为 max_results 为 25。如果添加 max_results 参数,则可以覆盖它。我用我的一个 Google 帐户尝试过,一次检索到 282 个联系人。
    猜你喜欢
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多