Dublay
Dublay

Reputation: 65

How to get Google search results displayed on a website using php?

I'm trying to get Googles search results displayed on my webpage, but I'm failing with some examples that I've found in the Google Docs. Here is what I've so far - this does not display any result. Any ideas?

<?php
$query = 'test';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&key=myKeyfromAPI&q=".$query;

$body = file_get_contents($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>";
}

?> 

Upvotes: 2

Views: 4039

Answers (1)

Mina Ezeet
Mina Ezeet

Reputation: 138

You have to register your own project to get The API Key.

You can register from the following link:

https://developers.google.com/

After you registered your own project activate Custom Search API then take your api key and add it here:

$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&key=myKeyfromAPI&q=".$query;

Upvotes: 3

Related Questions