Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A PHP library for accessing Google APIs available through the Google APIs Console. (In development, not yet stable)
PHP
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src/Google/Api
tests
LICENSE
README.md
phpunit.xml

README.md

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
Something went wrong with that request. Please try again.