In this article we’ll be reviewing Laravel Google Custom Search Engine package it allows you to easily add Google Custom Search within you laravel app . Well, there are not many possibilities if you want to have your own search engine on your site without too much programming. There were service called Swiftpe, where you had free plan – you just enter the URL of your site and you got HTML code to insert in you page – and you had your own fulltext search engine. Now there are only very expensive plans.
Nowadays, there is no simple solution, you cannot just import url or urls, but it is needed to convert them to documents, insert in some service like Algolia, learn the API and search and update by yourself, which is not very fast. You can use Google Custom Search engine the same way as Swiftype, but there are too much ads, which does not look very professional.
you can create Custom Search Engine for Laravel-project and use it in very simple way like
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize
$results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
Of course it is necessary to create Custom Search Engine and API key, which is once operation and it is described in detail:
Creating your custom search engine
- If you create your engine at https://cse.google.com/cse/ you will find the ID after you click at Settings
- Just check the URL you have like https://cse.google.com/cse/setup/basic?cx=search_engine_id and the string after cx= is your search engine ID
!! Attention !! If you change style of your Custom search engine, the ID can be changed
Get your API key
- go to https://console.developers.google.com, than
- click on the menu on the right side of the GoogleAPI logo and click on ‘Create project’
- enter the name of the new project – it is up to you, you can use ‘Google CSE’
- wait until project is created – the indicator is color circle on the top right corner around the bell icon
- API list is shown – search for ‘Google Custom Search API’ and click on it
- click on ‘Enable’ icone on the right side of Custom Search API headline
- click on the ‘Credentials’ on the left menu under the ‘Library’ section
- click on the ‘Create credentials’ and choose ‘API key’
- your API key is shown, so copy and paste it here
Save the configuration values
Save search engine ID and api ID in your config/laravelGoogleCustomSearchEngine
Author will update the package with cache as you can query free to Google API only 100 request / day, which is suitable for small and middle-sized projects, but not for bigger ones, so I will pack all query to cache, because the site is not changed in 24 hours, so it is not necessary to query the same phrases from Google API, but I need some time to do that.