4

運が悪ければ、Googleの連絡先から連絡先の名を取得しようとしています。ただし、メールアドレスは問題なく抽出できます。誰かが私が間違っていることを教えてもらえますか?

$xmlresponse=file_get_contents('https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken);
//reading xml using SimpleXML
$xml=  new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');

$nameFirst = $xml->xpath('//gd:givenName'); // I have also tried //gd:name
$result = $xml->xpath('//gd:email');

foreach($nameFirst as $nameF){
echo $nameF->getName();
}
foreach ($result as $title) {
echo $title->attributes()->address . "<br>";
}
?>
4

3 に答える 3

2

上記の例 (Google クライアント API の例から) は、メールでは機能しません。私は多くのことを試しましたが、応答には他の情報が含まれていますが、電子メールは含まれていません。彼らがそれについて話しているGoogleグループの議論を見つけました .simplexmlがいくつかのgd:情報を見ないバグのようです.

Claude と同じように simpleXMLElements と xpath を使用しましたが、メールしか取得できません。

于 2012-10-22T13:51:45.407 に答える
0

Googleが提供するさまざまなサービスと対話するために提供する PHP クライアント側ライブラリがあります。コンタクトもその一つです。

連絡先サービスのコード例では、json のエンコードと結果のデコードのトリックを使用しています。

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.
  $response = json_encode(simplexml_load_string($val->getResponseBody()));
  print "<pre>" . print_r(json_decode($response, true), true) . "</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>";
}
于 2012-07-12T06:46:59.967 に答える