#Laravel Google Custom Search Engine Laravel package to get Google Custom Search results from Google Custom Search Engine API for both free and paid version.
As Swiftype closed free plans, I started to find some alternative without too much coding, but was unsucessfull. The best I found was Spatie's Google Search package for Google CSE paid version, so I made some research and develop package similar way, but independent to Google CSE version.
If you like this project, you can buy me a coffee to help me get fresh. :) https://ko-fi.com/A067ES5
1/ Install with Composer
composer require jan-drda/laravel-google-custom-search-engine
2/ Add the service provider to config/app.php
'providers' => [
'...',
'JanDrdaLaravelGoogleCustomSearchEngineLaravelGoogleCustomSearchEngineProvider'
];
3/ Add alias for Facade to config/app.php
'aliases' => [
...
'GoogleCseSearch' => 'JanDrdaLaravelGoogleCustomSearchEngineFacadesLaravelGoogleCustomSearchEngineProvider',
...
]
4/ Publish the config file
php artisan vendor:publish --provider="JanDrdaLaravelGoogleCustomSearchEngineLaravelGoogleCustomSearchEngineProvider"
!! Attention !! If you change style of your Custom search engine, the ID can be changed
Save search engine ID and api ID in your config/laravelGoogleCustomSearchEngine.php
Create an object and call the function getResults to get first 10 results
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
It is only example controller name, you can use whatever you want, this is notice mainly for novices in Laravel
namespace AppHttpControllers;
use AppHttpControllersController;
use JanDrdaLaravelGoogleCustomSearchEngineLaravelGoogleCustomSearchEngine;
class GoogleSearchController extends Controller
{
public function index(){
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
}
}
You can also get information about the search like total records and search time
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
$info = $fulltext->getSearchInformation(); // get search information
You can use any parameter supported at Google. List of parameters is here: https://developers.google.com/custom-search/json-api/v1/reference/cse/list#parameters
E.g. you want to get next 10 results
$parameters = array(
'start' => 10 // start from the 10th results,
'num' => 10 // number of results to get, 10 is maximum and also default value
)
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
$results = $fulltext->getResults('some phrase', $parameters); // get second 10 results for query 'some phrase'
You can also get the raw result from Google including other information Full list of response variables is available here: https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
$rawResults = $fulltext->getRawResults(); // get complete response from Google
For getting the number of results only use
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
$noOfResults = $fulltext->getTotalNumberOfResults(); // get total number of results (it can be less than 10)
If you have more engines / more api keys, you can override the config variables with following functions
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
$fulltext->setEngineId('someEngineId'); // sets the engine ID
$fulltext->setApiKey('someApiId'); // sets the API key
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
Essetial documentation will be at Github Wiki Now is under the development.
This package is open-sourced software licensed under the MIT license
I am independent senior software consultant living in the Czech republic in IT business from 1997.