Google Custom Search Engine example in Codeigniter

In this tutorial I will show you how to integrate Google Custom Search Engine with Codeigniter.

Prerequisites

PHP Engine 5.4 or later
Codeigniter 2.2
Apache http server 2.2 or later
Knowledge of html, css, javascript, php

Final Output

Google CSE with Codeigniter

Step 1. Create google account if you do not have already one or sign in to google cse(Custom Search Engine) using your existing google account https://www.google.com/cse/

Step 2. Once you sign in to using google account, you will see similar page as shown below.

Google CSE with Codeigniter

Step 3. Click on New Search Engine link on the left upper corner.

Google CSE with Codeigniter

Now you may find examples for input fields. So input your site’s home page URL for Sites to search or you may also use as per directions given below the input field.

Also input some meaningful value for Name of the search engine. This field automatically gets populated when you type for Sites to search.

Next click on blue button below CREATE.

Step 4. Now you will get a next page similar to the below screen-shot.

Google CSE with Codeigniter

You can click on Get Code to copy the code and paste it on your site’s page where to appear.

You can click on Public URL to see the demo how your google custom search engine looks on web page.

You can click on Control Panel to modify your newly created or old search engines.

Step 5 (Optional). Now click on Control Panel.

Google CSE with Codeigniter

Whatever you make changes here will be appear on the right preview pane.

Basic tab

You can change search engine name.

You can give a brief description for your search engine.

You can give some keywords for your search engine for better search ranking.

Make Money tab

Google CSE with Codeigniter

If you have Adsense account then you can turn ON for Search Engine Monetization.

Admin tab

Google CSE with Codeigniter

You can add users using this tab. Users will be able to only add their sites for refinements or promotions but will not be able to make money.

Indexing tab

Google CSE with Codeigniter

Using this tab you can specify which URL(s) to index. You can also specify the sitemap URL. Google will index pages from your site.

Advance tab

Google CSE with Codeigniter

Using this tab you can customize everything for your search engine.

Step 6. Now click on Get Code link to get the source code and paste it on appropriate page to display the search input and search results.

Google CSE with Codeigniter
For advance features you can click on Get V1 Code link on the bottom of the page.

Google CSE with Codeigniter

Step 7. Now we will see how we can integrate this code into Codeigniter. Now look into Codeigniter below project structure.

Google CSE with Codeigniter

So we will put Controller under Controllers folder, View file under Views folder.

Step 8. Create a Controller search.php and paste the below source code.

 

Step 9. Create a View search.php and put it under Views folder.

Step 10. Edit route.php under application/config folder and change the default controller as search in place of welcome.

 

Step 11. Now we will customize look and feel of the cse. Click on Edit Search Engine -> Look and feel.
Google CSE with Codeigniter

In the Layout tab click on Two Column because we want to appear search input in one area and search results in another area. You can choose as per your requirement and you can see the preview on right side. Now click on save.

If you want to customize other features you can do that using Themes, Customize etc. tabs.

Now click on Save & Get Code for V1 and copy the code for Search Box and paste it on header or sidebar on the view file. If you are using template then you can put it on the template common area like header or sidebar.

Google CSE with Codeigniter

Next click on Next:Get code for search results link which can be found just below the code which you just copied.

Google CSE with Codeigniter

Now paste this code snippets in search.php file where you want your search results need to be displayed.

Full code on search.php file. Note if you are using template then your search box code and search results code may appear in different places. You must replace the below code with your own google cse code.

 

You can run the application and see the result but for getting the search results you have to test it in live server.

That’s all. Thank you for your reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.