Add Google Custom Search Engine in laravel

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:

Configuration

Creating your custom search engine

  1. If you create your engine at https://cse.google.com/cse/ you will find the ID after you click at Settings
  2. 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
Recommended :  How to use Laravel Socialite in depth

!! Attention !! If you change style of your Custom search engine, the ID can be changed

Get your API key

  1. go to https://console.developers.google.com, than
  2. click on the menu on the right side of the GoogleAPI logo and click on ‘Create project’
  3. enter the name of the new project – it is up to you, you can use ‘Google CSE’
  4. wait until project is created – the indicator is color circle on the top right corner around the bell icon
  5. API list is shown – search for ‘Google Custom Search API’ and click on it
  6. click on ‘Enable’ icone on the right side of Custom Search API headline
  7. click on the ‘Credentials’ on the left menu under the ‘Library’ section
  8. click on the ‘Create credentials’ and choose ‘API key’
  9. your API key is shown, so copy and paste it here
Recommended :  How to Generate sitemap in laravel

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.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares