2

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": "&quot;The Tank&quot;, 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"
     }
squidg
  • 327
  • 3
  • 9
  • 23
  • 1
    http://stackoverflow.com/questions/14055197/how-to-get-all-google-search-results-using-api – L. Vadim Jan 02 '17 at 12:31
  • 1
    Once you create a question, offer a bounty and then get a response you acknowledge as correct, don't change the question to a new one. Accept the helpful answer if correct (or flag why it's not correct) and create a new question for the new info you need. Otherwise the history is not helpful for other users (question and answer don't match) and you're not giving the user that answered their just rewards... – Robbie Jan 09 '17 at 04:40
  • Thanks for letting me know Robbie. I have changed everything back to how it should be. – squidg Jan 09 '17 at 06:29
  • Nicely done - thanks. Now post your next question - add the link in here and I'll visit and answer (if I can). – Robbie Jan 11 '17 at 03:54
  • http://stackoverflow.com/questions/41542147/output-google-search-web-results-only-results-using-php many thanks. – squidg Jan 12 '17 at 01:31

2 Answers2

6

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);
   }
}
Soenhay
  • 3,958
  • 5
  • 34
  • 60
Adam Fischer
  • 1,075
  • 11
  • 23
4

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);
sujivasagam
  • 1,659
  • 1
  • 14
  • 26
  • 1
    If you use the above method to do this, be prepared to get blacklisted by Google rather quickly as this is strictly against their TOS. – Chris Michaelides Nov 13 '17 at 20:09