Hello, I have following problem: I try to decode JSON coded results returned by Gooogle API (search API) in PHP. All goes well and I got JSON results but I don't know how to read this and interpret to display correct and formated in HTML. More details here: http://forums.digitalpoint.com/showthread.php?t=1638523 Please someone good in PHP let me know how to approach this, I will find our the rest no problem. Cheers!
It depends on what your looking to print to html, myself I use it for estimated results count. $responseobject = json_decode($response, true); error_log(print_r($responseobject, true)); $googlewebsearch = $responseobject['responseData']['cursor']['estimatedResultCount']; return $googlewebsearch; PHP: If you post exactly what your looking for, then maybe I can help further ? BTW are you using an API key ?
I am not using api key! I found an example on google for api search in php! Problem is i decode the json results but i dont know how i can use them and display results formated in html!!
Create a php file called searchexample.php load the following 3 php snippets. define('GOOGLE_API_KEY', 'ENTER CODE HERE'); // get one here http://code.google.com/apis/ajaxsearch/signup.html PHP: function curl_get($url, $params) { $post_params = array(); foreach ($params as $key => &$val) { if (is_array($val)) $val = implode(',', $val); $post_params[] = $key.'='.urlencode($val); } $post_string = implode('&', $post_params); $fullurl = $url."?".$post_string; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_URL, $fullurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mailana (curl)'); $result = curl_exec($ch); curl_close($ch); return $result; } PHP: function perform_google_web_search($termstring) { $start = 0; $result = array(); while ($start<50) { $searchurl = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0'; $searchurl .= '&key='.GOOGLE_API_KEY; $searchurl .= '&start='.$start; $searchurl .= '&rsz=large'; $searchurl .= '&filter=0'; $searchurl .= '&q='.urlencode($termstring); $response = curl_get($searchurl, array()); $responseobject = json_decode($response, true); if (count($responseobject['responseData']['results'])==0) break; $allresponseresults = $responseobject['responseData']['results']; foreach ($allresponseresults as $responseresult) { $result[] = array( 'url' => $responseresult['url'], 'title' => $responseresult['title'], 'abstract' => $responseresult['content'], ); } $start += 8; } return $result; } if (isset($_REQUEST['q'])) { $termstring = urldecode($_REQUEST['q']); } else { $termstring = ''; } ?> PHP: <html> <head> <title>Google Search Results</title> </head> <body> <div style="padding:20px;"> <center> <form method="GET" action="searchexample.php"> Search terms: <input type="text" size="40" name="q" value='<?=$termstring?>'/> </form> </center> </div> <?php if ($termstring!='') { $googleresults = perform_google_web_search($termstring); print '<br/><br/><h2>Google search results ('.count($googleresults).')</h2><br/>'; foreach ($googleresults as $result) { print '<a href="'.$result['url'].'">'.$result['title'].'</a><br/>'; print '<span style="font-size:80%">'.$result['abstract'].'</span><br/><hr/>'; } } ?> Code (markup):
Thanks so much MyVodaFone, unfortunately I will need to apply for API KEY I opted for my example because it did not required a key, I will see how this work and see from there. If you have other ideas all are welcomed. Have a great day, R.
I got the key from Google and used in your example. Unfortunately first form is ok, I input some words in search form but I got a blank screen as results. Thank you for helping, Regards.
I have tested the following and it works fine, simple create a file (any name).php enter your API code (KEY CODE HERE) remember you must run the code on the domain name you entered when applying for the API Key. <?php define('GOOGLE_API_KEY', 'KEY CODE HERE'); function curl_get($url, $params) { $post_params = array(); foreach ($params as $key => &$val) { if (is_array($val)) $val = implode(',', $val); $post_params[] = $key.'='.urlencode($val); } $post_string = implode('&', $post_params); $fullurl = $url."?".$post_string; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_URL, $fullurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mailana (curl)'); $result = curl_exec($ch); curl_close($ch); return $result; } function perform_google_web_search($termstring) { $start = 0; $result = array(); while ($start<50) { $searchurl = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0'; $searchurl .= '&key='.GOOGLE_API_KEY; $searchurl .= '&start='.$start; $searchurl .= '&rsz=large'; $searchurl .= '&filter=0'; $searchurl .= '&q='.urlencode($termstring); $response = curl_get($searchurl, array()); $responseobject = json_decode($response, true); if (count($responseobject['responseData']['results'])==0) break; $allresponseresults = $responseobject['responseData']['results']; foreach ($allresponseresults as $responseresult) { $result[] = array( 'url' => $responseresult['url'], 'title' => $responseresult['title'], 'abstract' => $responseresult['content'], ); } $start += 8; } return $result; } if (isset($_REQUEST['q'])) { $termstring = urldecode($_REQUEST['q']); } else { $termstring = ''; } ?> <html> <head> <title>Google</title> </head> <body> <div style="padding:20px;"> <center> <form method="GET" action=""> Search terms: <input type="text" size="40" name="q" value='<?=$termstring?>'/> </form> </center> </div> <?php if ($termstring!='') { $googleresults = perform_google_web_search($termstring); print '<br/><br/><h2>Google search results</h2><br/>'; foreach ($googleresults as $result) { print '<a href="'.$result['url'].'">'.$result['title'].'</a><br/>'; print '<span style="font-size:80%">'.$result['abstract'].'</span><br/><hr/>'; } } ?> PHP:
It worked !! Thanks so much MyVodaFone, your help is much appreciated. I will now process the results in the way I want but this was the hardest part. Cheers
I have same problem its shows me only blank site, I am using localhost to test this php Google Search Api. Api Key is done and included in code. any help? Creetings mmj.
Note that Google Search API si deprecated and since jan 2013 is not supported anymore you should have a look at custom search api instead...
Thx for reply I found that site very complex but I will try study it. But do you have any easy example for that Google Custom Search api?
You can also do something like this: $query = [ "q" => "Coffee", "google_domain" => "google.com", ]; $serp = new GoogleSearchResults(); $json_results = $serp.json($query); Code (markup): GitHub: https://github.com/serpapi/google-search-results-php