@rollin
To import contacts from Gmail using PHP, you can use Google's People API. Here is a step-by-step guide on how to do this:
1
|
composer require google/apiclient:^2.0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php require_once 'vendor/autoload.php'; $client = new Google_Client(); $client->setAuthConfig('path/to/credentials.json'); $client->addScope('https://www.googleapis.com/auth/contacts.readonly'); if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); $client->setAccessToken($token); } if ($client->isAccessTokenExpired()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); } $service = new Google_Service_PeopleService($client); $connections = $service->people_connections->listPeopleConnections('people/me', [ 'personFields' => 'names,emailAddresses', ]); foreach ($connections->getConnections() as $person) { $names = $person->getNames(); $emailAddresses = $person->getEmailAddresses(); $name = $names[0]->getDisplayName(); $email = $emailAddresses[0]->getValue(); echo "Name: $name, Email: $email <br>"; } |
1
|
php -S localhost:8000 |
That's it! You have successfully imported contacts from Gmail using PHP.