0

我正在使用谷歌最新的 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>";
}
?>

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

首先你需要询问版本 3,因为extendedProperties 没有返回。这是来自一些工作代码的cnp。

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;
    }
  } 
4

1 回答 1

2

您是否尝试过使用 max-results 查询参数?来自联系人 API 文档

注意:Feed 可能不包含用户的所有联系人,因为返回的结果数量存在默认限制。有关更多信息,请参阅使用查询参数检索联系人中的 max-results 查询参数。

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

于 2012-07-12T21:09:34.143 回答