dev-master
9999999-devGoogle Custom Search api
The Development Requires
by Xiaobin
Wallogit.com
2017 © Pedro Peláez
Google Custom Search api
The aim of this project is to create a well structured PHP library for accessing Google APIs available through the Google APIs console., (*1)
Currently the library enables you to interact programmatically with the following Google APIs,, (*2)
Simply download the library and add the src folder to your project., (*3)
Note: More extensive documentation will be made available at a later date., (*4)
Note: As well as an API key, the Google Custom Search API v1 also requires either a [Google Custom Search][5] ID or specification URL., (*5)
The following makes a simple Google Custom Search API v1 request,, (*6)
$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,, (*7)
if ($response->isSuccess())
{
foreach($response->getData()->getItems() as $item)
{
echo $item->getHtmlTitle(), ' - <a href="', $item->getLink(), '">', $item->getDisplayLink(), '</a><br />';
}
}
The following makes a simple Google Translate API v2 request,, (*8)
$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,, (*9)
if ($response->isSuccess())
{
foreach($response->getData()->getTranslations() as $translation)
{
echo $translation->getTranslatedText(), '<br />';
}
}
To run the tests, make sure you have PHPUnit 3.6.0 and up installed, and just run the following in the project root,, (*10)
phpunit
Google Custom Search api