google + oAuth联系PHP中的完整列表

时间:2012-07-12 20:11:41

标签: php oauth google-plus

我正在使用googles最新的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;
    }
  } 

1 个答案:

答案 0 :(得分:2)

您是否尝试过使用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