Vineeth Omalloor
Vineeth Omalloor

Reputation: 21

Google Search with codeigniter

I am developing an application in codeigniter.In my application there is a need to integrate with Google search.In my application there is a textbox and a submit button,If the user searches something it will search the data in google and display the result in my page. How can i do it?Any idea from anyone.

Upvotes: 0

Views: 1480

Answers (1)

Parag Tyagi
Parag Tyagi

Reputation: 8960

I've successfully used,

<?php
$query = "Steve Jobs";
$api_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&&rsz=large&q=".$query;

$body = file_get_contents($api_url);
$json = json_decode($body);

for($x=0;$x<count($json->responseData->results);$x++)
{
  echo "<b>Result ".($x+1)."</b>";
  echo "<br>URL: ";
  echo $json->responseData->results[$x]->url;
  echo "<br>VisibleURL: ";
  echo $json->responseData->results[$x]->visibleUrl;
  echo "<br>Title: ";
  echo $json->responseData->results[$x]->title;
  echo "<br>Content: ";
  echo $json->responseData->results[$x]->content;
  echo "<br><br>";
}


In Codeigniter you can create a helper function (if to be used many times) for this,

applications/helpers/google_helper.php

<?php
function google_search($query)
{
    $api_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&&rsz=large&q=".$query;

    $body = file_get_contents($api_url);
    return json_decode($body);
}

Upvotes: 2

Related Questions