williancarminato / google-cse-bundle
A Symfony2 Bundle who uses Google Custom Search API
Installs: 848
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 2
Open Issues: 3
Type:symfony-bundle
Requires
- php: >=5.3.3
- symfony/framework-bundle: ~2.4
This package is not auto-updated.
Last update: 2025-03-11 06:52:47 UTC
README
A Symfony2 Bundle who uses Google Custom Search API.
Feature
- Google Custom Search feature, see CSE
Requirements
- PHP 5.4+
- Search engine ID (Creating a Custom Search Engine)
- API key that can be obtained from the Google Cloud Console
Instalation
Package available on Packagist. Autoloading with Composer is PSR-0 compatible.
Usage
Let's suppose that you have a form with the input text 'search_term'. In your controller you can get the results after form submission like this:
<?php ... use Carminato\GoogleCseBundle\Service\ApiRequest; use Carminato\GoogleCseBundle\Service\Query\ApiQuery; class SearchController extends Controller { ... public function searchAction(Request $request) { $filter = $this->createForm(new SearchFilterType()); $filter->bind($request); if ($filter->isValid()) { $data = $filter->getData(); $apiQuery = new ApiQuery( array( 'key' => API_KEY, 'cx' => CX_KEY, 'q' => $data['search_term'], 'start' => 1, 'userIp' => $request->getClientIp() ) ); $apiRequest = new ApiRequest($apiQuery); $response = $apiRequest->getResponse(); if ($error = $response->getErrors()) { return new Response($error->getMessage(), $error->getCode()); } return array( 'results' => $response->getResults() ); } ... } }
The results is an array containing CseApiResultItem objects.