1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP and JSON decode for Google API

Discussion in 'PHP' started by shmekerosu, Jan 6, 2010.

  1. #1
    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.
    SEMrush
    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!
     
    shmekerosu, Jan 6, 2010 IP
    SEMrush
  2. AsHinE

    AsHinE Active Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    88
    #2
    AsHinE, Jan 6, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    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 ?
     
    MyVodaFone, Jan 6, 2010 IP
  4. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #4
    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!!
     
    shmekerosu, Jan 6, 2010 IP
  5. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Btw do you know other better way to use google api for search in php??
     
    shmekerosu, Jan 6, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    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):
     
    MyVodaFone, Jan 6, 2010 IP
  7. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #7
    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.
     
    shmekerosu, Jan 6, 2010 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    Pending on your internet speed, you will have a key in 30seconds.
     
    MyVodaFone, Jan 6, 2010 IP
  9. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #9
    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.
     
    shmekerosu, Jan 8, 2010 IP
  10. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #10
    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:
     
    MyVodaFone, Jan 9, 2010 IP
  11. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #11
    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
     
    shmekerosu, Jan 10, 2010 IP
  12. Marko Ylitalo

    Marko Ylitalo Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #12
    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.
     
    Marko Ylitalo, Jan 30, 2013 IP
  13. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #13
    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...
     
    shmekerosu, Jan 30, 2013 IP
  14. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #14
    MyVodaFone, Jan 30, 2013 IP
  15. Marko Ylitalo

    Marko Ylitalo Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #15
    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?
     
    Marko Ylitalo, Jan 30, 2013 IP
  16. shmekerosu

    shmekerosu Active Member

    Messages:
    571
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    90
    #16
    I have some example but not on hand right now, I will get back with Google CS example here...
     
    shmekerosu, Feb 4, 2013 IP
  17. sakabaro

    sakabaro Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #17
    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
     
    sakabaro, May 25, 2018 IP