944,007 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 5314
  • PHP RSS
Sep 15th, 2009
0

Google Search API???

Expand Post »
Hi and I have been looking around and found that google has a few search api's but haven't yet found one that returns the results as a javascript/ajax array. Is it possible to at least get the google api to send the results to my website as a variable/array in javascript/ajax/php because can't see any reference to this.
PS. This is in the php section because all results will eventually end up in php. Also I tried the file_get_contents() function and curl on the google homepage but google knows it's a bot.
Similar Threads
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 15th, 2009
1

Re: Google Search API???

According to the section "PHP Access" in http://code.google.com/apis/ajaxsear...on/#The_Basics it should be possible.

My guess is you already tried this. At work we use the paid version of google search, which just returns xml.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,182 posts
since Jul 2006
Sep 15th, 2009
0

Re: Google Search API???

From all the pages I clicked I didn't find that section. But does anybody know how to return 10 results instead of 4 ?
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 15th, 2009
0

Re: Google Search API???

http://code.google.com/apis/ajaxsear..._class_GSearch

See the "Searchers" section. Appears to be limited to 4 or 8.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,182 posts
since Jul 2006
Sep 15th, 2009
0

Re: Google Search API???

Any idea on how to implement that into the php script on the first link? I can't see how it fit's in but am trying to work it out.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 15th, 2009
0

Re: Google Search API???

I have solved it and will post some code soon.

Edit:
Here is the code which solved my problem:
php Syntax (Toggle Plain Text)
  1. $searchterm='Paris Hilton';
  2. $searchterm=urlencode($searchterm);
  3. $ch = curl_init();
  4. curl_setopt($ch, CURLOPT_URL, 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='.$searchterm.'&start=0');
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  6. curl_setopt($ch, CURLOPT_REFERER, 'http://www.syntax.cwarn23.info/');
  7. $body = curl_exec($ch);
  8. curl_close($ch);
  9. $string = json_decode($body);
  10. $dstring='';
  11. foreach ($string->responseData->results AS $val) {
  12. $dstring.='<<---<<>-><';
  13. $dstring.=str_replace(array('<<---<<>-><','<b>','</b>','<em>','</em>','<i>','</i>'),'',$val->title);
  14. $dstring.='<<---<<>-><';
  15. $dstring.=str_replace('<<---<<>-><','',$val->url);
  16. }
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='.$searchterm.'&start=4');
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20. curl_setopt($ch, CURLOPT_REFERER, 'http://www.syntax.cwarn23.info/');
  21. $bodyb = curl_exec($ch);
  22. curl_close($ch);
  23. $stringb = json_decode($bodyb);
  24. foreach ($stringb->responseData->results AS $valb) {
  25. $dstring.='<<---<<>-><';
  26. $dstring.=str_replace(array('<<---<<>-><','<b>','</b>','<em>','</em>','<i>','</i>'),'',$valb->title);
  27. $dstring.='<<---<<>-><';
  28. $dstring.=str_replace('<<---<<>-><','',$valb->url);
  29. }
  30. $dstring=substr($dstring,11);
  31. //echo '<br>'.$dstring;
  32. $array=explode ('<<---<<>-><',$dstring);
  33. echo '<xmp>';
  34. print_r($array);
  35. echo '</xmp>';
Last edited by cwarn23; Sep 15th, 2009 at 10:13 am. Reason: added code
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: why using double quotes..
Next Thread in PHP Forum Timeline: I need a little tweek to a page nav code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC