Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

Commit

Permalink
add Pagemap properties to result
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 17, 2014
1 parent c1ed891 commit 9e73287
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "~4.1",
"illuminate/support": "~4.1|5.*",
"guzzlehttp/guzzle": "~4.0"
},
"require-dev": {
Expand Down
35 changes: 33 additions & 2 deletions src/Spatie/GoogleSearch/GoogleSearch.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Spatie\GoogleSearch;

use Exception;
use Spatie\GoogleSearch\Interfaces\GoogleSearchInterface;
use GuzzleHttp\Client;

Expand All @@ -17,7 +18,8 @@ public function __construct($searchEngineId) {
*
* @param string $query
* @return array An associative array of the parsed comment, whose keys are `name`,
* `url` and `snippet`
* `url` and `snippet` and some others
* @throws Exception
*/
public function getResults($query) {

Expand All @@ -44,7 +46,7 @@ public function getResults($query) {


if ($result->getStatusCode() != 200) {
throw new \Exception('Resultcode was not ok: ' . $result->getStatusCode());
throw new Exception('Resultcode was not ok: ' . $result->getStatusCode());
}

$xml = simplexml_load_string($result->getBody());
Expand All @@ -55,10 +57,39 @@ public function getResults($query) {
$searchResults[$i]['name'] = (string)$item->T;
$searchResults[$i]['url'] = (string)$item->U;
$searchResults[$i]['snippet'] = (string)$item->S;
$searchResults[$i]['image'] = $this->getPageMapProperty($item, 'cse_image', 'src');

$searchResults[$i]['product']['name'] = $this->getPageMapProperty($item, 'product', 'name');
$searchResults[$i]['product']['brand'] = $this->getPageMapProperty($item, 'product', 'brand');
$searchResults[$i]['product']['price'] = $this->getPageMapProperty($item, 'product', 'price');
$searchResults[$i]['product']['image'] = $this->getPageMapProperty($item, 'product', 'image');
$searchResults[$i]['offer']['price'] = $this->getPageMapProperty($item, 'offer', 'price');
$searchResults[$i]['offer']['pricecurrency'] = $this->getPageMapProperty($item, 'offer', 'pricecurrency');

$i++;
}
}

return $searchResults;
}

/**
* Get a the value Pagemap property with the given name of the given type of the given item
*
* @param $item
* @param $type
* @param $attribute
* @return string
*/
public function getPageMapProperty($item, $type, $attribute)
{
$propertyArray = $item->PageMap->xpath('DataObject[@type="' . $type . '"]/Attribute[@name="' . $attribute . '"]/@value');

if (! count($propertyArray))
{
return '';
}

return (string)$propertyArray[0];
}
}

0 comments on commit 9e73287

Please sign in to comment.