orvice/google-custom-search-api

There is no license information available for the latest version (dev-master) of this package.

Google Custom Search api

dev-master 2016-04-15 14:47 UTC

This package is not auto-updated.

Last update: 2024-03-27 21:57:06 UTC


README

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