From 9e732875a5c993a904d298d64548edc09535a63c Mon Sep 17 00:00:00 2001 From: freek Date: Wed, 17 Dec 2014 11:46:57 +0100 Subject: [PATCH] add Pagemap properties to result --- composer.json | 2 +- src/Spatie/GoogleSearch/GoogleSearch.php | 35 ++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 4c65c72..7b62ab4 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ ], "require": { "php": ">=5.4.0", - "illuminate/support": "~4.1", + "illuminate/support": "~4.1|5.*", "guzzlehttp/guzzle": "~4.0" }, "require-dev": { diff --git a/src/Spatie/GoogleSearch/GoogleSearch.php b/src/Spatie/GoogleSearch/GoogleSearch.php index 6e982de..3770cb1 100755 --- a/src/Spatie/GoogleSearch/GoogleSearch.php +++ b/src/Spatie/GoogleSearch/GoogleSearch.php @@ -1,5 +1,6 @@ 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()); @@ -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]; + } } \ No newline at end of file