2016-07-29 9 views
6

私は友達の電子メールで完全な連絡先リストを取得したいと思います。 私は連絡先のリストを取得できますが、電子メールはありません。Google API - 連絡先メールを取得する

ここは私のコードです:

require_once APPPATH . 'vendor/google/src/Google_Client.php'; 
require_once APPPATH . 'vendor/google/src/contrib/Google_Oauth2Service.php'; 
$client = new Google_Client(); 

$client->setApplicationName("PHP Google Test"); 
$client->setClientId('xxx'); 
$client->setClientSecret('xxx'); 
$client->setRedirectUri('http://www.domain.xx/admin/others/google?test'); 
$client->setScopes("http://www.google.com/m8/feeds/"); 

$oauth2 = new Google_Oauth2Service($client); 

if (isset($_GET['code'])) { 
    $client->authenticate(); 
    $_SESSION['token'] = $client->getAccessToken(); 
    $redirect   = 'http://www.domain.xx/admin/others/google'; 
    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 Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full"); 
    $val = $client->getIo()->authenticatedRequest($req); 
    $response = json_encode(simplexml_load_string($val->getResponseBody())); 
    print "<pre>" . print_r(json_decode($response, true), true) . "</pre>"; 
    $_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $auth = $client->createAuthUrl(); 
} 

$auth = $client->createAuthUrl(); 

echo '<a href="' . $auth . '" target="_blank">' . $auth . '</a>'; 

私が連絡先リストから電子メールを取得できますか?

コード:

$response = json_encode(simplexml_load_string($val->getResponseBody())); 
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>"; 

変更するには:

+0

誰かが私を助けて参照し、Googleの連絡先のAPIからの問い合わせ種類の要素の詳細情報を探しますか? –

+0

これはまさに答えと同じ質問です:http://stackoverflow.com/questions/21469433/how-to-get-email-address-when-user-is-authenticated-with-google-oauth2-and-peopl – Jehy

+0

いいえ、私は連絡先から電子メールを取得する必要があります:) –

答えて

3

私は解決策持っ

$xml = simplexml_load_string($val->getResponseBody()); 
      $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');   

      $output_array = array(); 
      foreach ($xml->entry as $entry) { 
       foreach ($entry->xpath('gd:email') as $email) { 
       $output_array[] = array((string)$entry->title, (string)$email->attributes()->address); 
       } 
      } 
      print_r($output_array); 
0

は、あなたがGoogleの連絡先のAPIにGDATAコンポーネントからの問い合わせ種類を利用することができ $responseがあると仮定すると、リクエストからGoogleまでの連絡先の返信:

$xml= new SimpleXMLElement($response); 
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005'); 
$result = $xml->xpath('//gd:email'); 

here

関連する問題