cloudhal
cloudhal

Reputation: 134

Google API php client - retrieve search result details

I am having issues with the google https://github.com/google/google-api-php-client. All I want to do is get some details from search results using a custom search engine (CSE). The same thing took me 30 minutes using the bing API, but I cannot get this to work. The API call is working, I just cannot get the results from the returned object. Using the examples from the source above, the simple-query.php works fine using the books API (such a useful example!), and I can return details of the search results using e.g.

    echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
  echo $item['selfLink'],"<br /><br/> \n";
  echo $item['etag'],"<br /> \n";
  echo $item['volumeInfo']['title'],"<br /> \n";
  echo $item['volumeInfo']['authors']['0'],"<br /> \n";
}

This is using my API key and CSE ID.

However, if I try and use CSE, I am not able to use the results object which is returned. So modifying the simple search example:

<?php

include_once "templates/base.php";
echo pageHeader("Custom Image Search");

//API request
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Customsearch.php';

//Create the client
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Enter your API key
// Warn if the API key isn't changed.
if ($apiKey == '<YOUR_API_KEY>') {
  echo missingApiKeyWarning();
}
$client->setDeveloperKey($apiKey);

$service = new Google_Service_Customsearch($client);

//Do the query
$query = 'donkey';
$optParams = array(
        'imgSize' => 'large',
        'searchType' => 'image',
        'num' => '5',
        'safe' => 'medium',
        'cx' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //Added your cx or search engine ID here, see https://cse.google.com/cse/
        );
$results = $service->cse->listCse($query, $optParams);



//Iterate over the results
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
  echo "Test <br/>";
  echo $item['link'], "<br /> \n";
}

This just returns nothing.

Using var_dump on $results I can see that it is returning the results of the search (yes I am searching for images of donkeys, just as an example!).

But using the books example, I can see that there are 6 results by doing:

echo "Results count: " .count($results);

But doing this with the cse example returns 0. So unsurprisingly I am iterating over nothing.

So, can anyone tell me how I can actually get the results using the php client and CSE?

Thanks

Upvotes: 3

Views: 1402

Answers (2)

phobia82
phobia82

Reputation: 1257

You should use the methods included in the API to retrieve results and items details:

    foreach($results->getItems() as $k=>$item){
        echo $item->getCacheId()."<br/>";
        echo $item->getDisplayLink()."<br/>";
        echo $item->getFileFormat()."<br/>";
        echo $item->getFormattedUrl()."<br/>";
        echo $item->getHtmlFormattedUrl()."<br/>";
        echo $item->getHtmlTitle()."<br/>";
        echo $item->getImage()."<br/>";
        echo $item->getKind()."<br/>";
        echo json_encode($item->getLabels())."<br/>";
        echo $item->getLink()."<br/>";
        echo $item->getMime()."<br/>";
        echo json_encode($item->getPagemap())."<br/>";
        echo $item->getSnippet()."<br/>";
        echo $item->getTitle()."<br/>";
    }

Upvotes: 2

cloudhal
cloudhal

Reputation: 134

OK this is how I did this in the end, just made it into an array and then used array_walk_recursive:

//Making the returned object into an array
$array =  (array) $results;
function array_print($item, $key)
{
     if($key=='link'){
       echo $item ."<br>";
     }
}

array_walk_recursive($array, 'array_print');

Upvotes: 0

Related Questions