Skip to content

A PHP library for accessing Google APIs available through the Google APIs Console. (In development, not yet stable)

License

Notifications You must be signed in to change notification settings

jg314/google-apis-php-clients

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google APIs PHP Clients

The aim of this project is to create a well structured PHP library for accessing Google APIs available through the Google APIs console.

Currently the library enables you to interact programmatically with the following Google APIs,

Requirements

  • The library is only supported on PHP 5.3.0 and up.
  • It has been assumed an autoloader will be present. If you require one, you can find one here.
  • Each API requires an API key, which you can get from the Google APIs console.

Installation

Simply download the library and add the src folder to your project.

Usage

Note: More extensive documentation will be made available at a later date.

Custom Search API

Note: As well as an API key, the Google Custom Search API v1 also requires either a Google Custom Search ID or specification URL.

The following makes a simple Google Custom Search API v1 request,

$apiClient = new \Google\Api\CustomSearch();
$apiClient->setApiKey('INSERT_YOUR_API_KEY_HERE');
$apiClient->setCustomSearchEngineId('INSERT_YOUR_CUSTOM_SEARCH_ENGINE_ID_HERE');
$apiClient->setQuery('flowers');

$response = $apiClient->executeRequest();

To get the results from the $response,

if ($response->isSuccess())
{
    foreach($response->getData()->getItems() as $item)
    {
        echo $item->getHtmlTitle(), ' - <a href="', $item->getLink(), '">', $item->getDisplayLink(), '</a><br />';
    }
}

Translate API

The following makes a simple Google Translate API v2 request,

$apiClient = new \Google\Api\Translate();
$apiClient->setApiKey('INSERT_YOUR_API_KEY_HERE');
$apiClient->addSourceText('The quick brown fox jumps over the lazy dog.');
$apiClient->setTargetLanguage('fr');

$response = $apiClient->executeRequest();

To get the translations from the $response,

if ($response->isSuccess())
{
    foreach($response->getData()->getTranslations() as $translation)
    {
        echo $translation->getTranslatedText(), '<br />';
    }
}

Testing

To run the tests, make sure you have PHPUnit 3.6.0 and up installed, and just run the following in the project root,

phpunit

About

A PHP library for accessing Google APIs available through the Google APIs Console. (In development, not yet stable)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published