Cześć.

Potrzebuję pobrać adresy email z kontaktów.
Udało mi się pobrać kontakty ale niestety dostaje drzewko bez maila (jest imię i nazwisko, id).

Wiecie jak pobrać email kontaktu z konta google przez api?

Oto mój kod który pobiera kontakty:

  1. require_once APPPATH . 'vendor/google/src/Google_Client.php';
  2. require_once APPPATH . 'vendor/google/src/contrib/Google_Oauth2Service.php';
  3. $client = new Google_Client();
  4.  
  5. $client->setApplicationName("PHP Google Test");
  6. $client->setClientId('xxx');
  7. $client->setClientSecret('xxx');
  8. $client->setRedirectUri('http://www.domain.xx/admin/others/google?test');
  9. $client->setScopes("http://www.google.com/m8/feeds/");
  10.  
  11. $oauth2 = new Google_Oauth2Service($client);
  12.  
  13. if (isset($_GET['code'])) {
  14. $client->authenticate();
  15. $_SESSION['token'] = $client->getAccessToken();
  16. $redirect = 'http://www.domain.xx/admin/others/google';
  17. header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  18. }
  19. if (isset($_SESSION['token'])) {
  20. $client->setAccessToken($_SESSION['token']);
  21. }
  22. if (isset($_REQUEST['logout'])) {
  23. unset($_SESSION['token']);
  24. $client->revokeToken();
  25. }
  26. if ($client->getAccessToken()) {
  27. $req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
  28. $val = $client->getIo()->authenticatedRequest($req);
  29. $response = json_encode(simplexml_load_string($val->getResponseBody()));
  30. print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";
  31. $_SESSION['token'] = $client->getAccessToken();
  32. } else {
  33. $auth = $client->createAuthUrl();
  34. }
  35.  
  36. $auth = $client->createAuthUrl();
  37.  
  38. echo '<a href="' . $auth . '" target="_blank">' . $auth . '</a>';


I otrzymuję przykład:

  1. [24] => Array
  2. (
  3. [id] => <a href="http://www.google.com/m8/feeds/contacts/xxxx@gmail.com/base/xxx" target="_blank">http://www.google.com/m8/feeds/contacts/xx...il.com/base/xxx</a>
  4. [updated] => 2016-03-13T21:13:40.065Z
  5. [category] => Array
  6. (
  7. [@attributes] => Array
  8. (
  9. [scheme] => <a href="http://schemas.google.com/g/2005#kind" target="_blank">http://schemas.google.com/g/2005#kind</a>
  10. [term] => <a href="http://schemas.google.com/contact/2008#contact" target="_blank">http://schemas.google.com/contact/2008#contact</a>
  11. )
  12.  
  13. )
  14.  
  15. [title] => Array
  16. (
  17. [@attributes] => Array
  18. (
  19. [type] => text
  20. )
  21.  
  22. )
  23.  
  24. [link] => Array
  25. (
  26. [0] => Array
  27. (
  28. [@attributes] => Array
  29. (
  30. [rel] => <a href="http://schemas.google.com/contacts/2008/rel#edit-photo" target="_blank">http://schemas.google.com/contacts/2008/rel#edit-photo</a>
  31. [type] => image/*
  32. [href] => <a href="https://www.google.com/m8/feeds/photos/media/xxxx@gmail.com/xxx/xxxx" target="_blank">https://www.google.com/m8/feeds/photos/medi...il.com/xxx/xxxx</a>
  33. )
  34.  
  35. )
  36.  
  37. [1] => Array
  38. (
  39. [@attributes] => Array
  40. (
  41. [rel] => self
  42. [type] => application/atom+xml
  43. [href] => <a href="https://www.google.com/m8/feeds/contacts/xxxx@gmail.com/full/xxx" target="_blank">https://www.google.com/m8/feeds/contacts/xx...il.com/full/xxx</a>
  44. )
  45.  
  46. )
  47.  
  48. [2] => Array
  49. (
  50. [@attributes] => Array
  51. (
  52. [rel] => edit
  53. [type] => application/atom+xml
  54. [href] => <a href="https://www.google.com/m8/feeds/contacts/xxxx@gmail.com/full/xxx/xxxx" target="_blank">https://www.google.com/m8/feeds/contacts/xx...m/full/xxx/xxxx</a>
  55. )
  56.  
  57. )
  58.  
  59. )
  60.  
  61. )


Proszę o pomoc

ktoś pomoże? (dość pilna sprawa)

Nikt nie jest w stanie mi pomóc?

Cześć.

Udało mi się rozwiązać ten problem.

Fragment:

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


Zamieniamy na:

  1. $xml = simplexml_load_string($val->getResponseBody());
  2. $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
  3.  
  4. $output_array = array();
  5. foreach ($xml->entry as $entry) {
  6. foreach ($entry->xpath('gd:email') as $email) {
  7. $output_array[] = array((string)$entry->title, (string)$email->attributes()->address);
  8. }
  9. }
  10. print_r($output_array);