Reputation: 357
I have looked at several old answers on stackoverflow but they are all out-dated and the API they used is no longer available.
I have created a JSON/Atom API, CX key and used a script Thanks to Adam Fischer I found on here but when I'm trying am now able to output print the results on the page I'm getting with the error:
Notice: Undefined property: stdClass::$responseData in E:\XAMPP\htdocs\PHP Training\google.php on line 19
Notice: Trying to get property of non-object in E:\XAMPP\htdocs\PHP Training\google.php on line 19
This is what I have so far. below code.
$url = 'https://www.googleapis.com/customsearch/v1?key=[MY API KEY]&cx=[MY CX KEY]&q=lecture';
$body = file_get_contents($url);
$json = json_decode($body);
for($x=0;$x<countif ($json->responseData->results);$x++>items){
echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echoforeach ($json->responseData->results[$x]->url;
echo>items "<br>VisibleURL:as ";$item){
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";print_r($item)
echo $json->responseData->results[$x]->content;
echo "<br><br>"; }
}
The API is working correctly because when I visit This spits out everything in an array. Example: dl.dropboxusercontent.com/u/47731225/sample.txt
I'm trying to make the $url I see results such as be displayed on my page like a Google Search, for example: prntscr.com/drum5u
{
"kind": "customsearch#result",
"title": "The Tank, Haydon Allen Lecture Theatre, Building 23, ANU",
"htmlTitle": "The Tank, Haydon Allen \u003cb\u003eLecture\u003c/b\u003e Theatre, Building 23, ANU",
"link": "https://www.google.com/mymaps/viewer?mid=1YGFZHcZ20jPvy5OiaKT1voy841Q&hl=en",
"displayLink": "www.google.com",
"snippet": "\"The Tank\", Haydon Allen Lecture Theatre, Building 23, The Australian National \nUniversity (ANU), Canberra, Australia.",
"htmlSnippet": ""The Tank", Haydon Allen \u003cb\u003eLecture\u003c/b\u003e Theatre, Building 23, The Australian National \u003cbr\u003e\nUniversity (ANU), Canberra, Australia.",
"cacheId": "hTeucZ5TewoJ",
"formattedUrl": "https://www.google.com/mymaps/viewer?mid...hl=en",
"htmlFormattedUrl": "https://www.google.com/mymaps/viewer?mid...hl=en",
"pagemap": {
"cse_thumbnail": [
{
"width": "221",
"height": "228",
"src": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSntx5YhQgJQeJ6RAZajOx7SGOwh0oUu8jtpY6VOAS75V_oNkiXx923ro4"
}
Upvotes: 2
Views: 19279
Reputation: 1100
Did you look through the returned json from API? My guess is, that it is completely different, from what you expect
See
https://developers.google.com/custom-search/json-api/v1/reference/cse/list
After clarifiacation, you result is realy different from what your code expected.
Correct code should look like
$url = 'https://www.googleapis.com/customsearch/v1?key=[MY API KEY]&cx=[MY CX KEY]&q=lecture';
$body = file_get_contents($url);
$json = json_decode($body);
if ($json->items){
foreach ($json->items as $item){
print_r($item);
}
}
Upvotes: 8
Reputation: 1769
You can use the file get content to get the full page content of the google and you can display the result in your site like
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$query = "search term";
$url = 'http://www.google.co.in/search?q='.urlencode($query).'';
$scrape = file_get_contents_curl($url);
Upvotes: 4