1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\GoogleSearch; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Spatie\GoogleSearch\Interfaces\GoogleSearchInterface; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
|
9
|
|
|
class GoogleSearch implements GoogleSearchInterface |
10
|
|
|
{ |
11
|
|
|
protected $searchEngineId; |
12
|
|
|
protected $query; |
13
|
|
|
|
14
|
|
|
public function __construct($searchEngineId) |
15
|
|
|
{ |
16
|
|
|
$this->searchEngineId = $searchEngineId; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get results from a Google Custom Search Engine. |
21
|
|
|
* |
22
|
|
|
* @param string $query |
23
|
|
|
* |
24
|
|
|
* @return array An associative array of the parsed comment, whose keys are `name`, |
25
|
|
|
* `url` and `snippet` and some others |
26
|
|
|
* |
27
|
|
|
* @throws Exception |
28
|
|
|
*/ |
29
|
|
|
public function getResults($query) |
30
|
|
|
{ |
31
|
|
|
$searchResults = []; |
32
|
|
|
|
33
|
|
|
if ($query == '') { |
34
|
|
|
return $searchResults; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($this->searchEngineId == '') { |
38
|
|
|
throw new \Exception('You must specify a searchEngineId'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$client = new Client(); |
42
|
|
|
$result = $client->get('http://www.google.com/cse', ['query' => ['cx' => $this->searchEngineId, |
43
|
|
|
'client' => 'google-csbe', |
44
|
|
|
'num' => 20, |
45
|
|
|
'output' => 'xml_no_dtd', |
46
|
|
|
'q' => $query, |
47
|
|
|
], |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
if ($result->getStatusCode() != 200) { |
51
|
|
|
throw new Exception('Resultcode was not ok: '.$result->getStatusCode()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$xml = simplexml_load_string($result->getBody()); |
55
|
|
|
|
56
|
|
|
if ($xml->ERROR) { |
57
|
|
|
throw new Exception('XML indicated service error: '.$xml->ERROR); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($xml->RES->R) { |
61
|
|
|
$i = 0; |
62
|
|
|
foreach ($xml->RES->R as $item) { |
63
|
|
|
$searchResults[$i]['name'] = (string) $item->T; |
64
|
|
|
$searchResults[$i]['url'] = (string) $item->U; |
65
|
|
|
$searchResults[$i]['snippet'] = (string) $item->S; |
66
|
|
|
$searchResults[$i]['image'] = $this->getPageMapProperty($item, 'cse_image', 'src'); |
67
|
|
|
|
68
|
|
|
$searchResults[$i]['product']['name'] = $this->getPageMapProperty($item, 'product', 'name'); |
69
|
|
|
$searchResults[$i]['product']['brand'] = $this->getPageMapProperty($item, 'product', 'brand'); |
70
|
|
|
$searchResults[$i]['product']['price'] = $this->getPageMapProperty($item, 'product', 'price'); |
71
|
|
|
$searchResults[$i]['product']['image'] = $this->getPageMapProperty($item, 'product', 'image'); |
72
|
|
|
$searchResults[$i]['product']['identifier'] = $this->getPageMapProperty($item, 'product', 'identifier'); |
73
|
|
|
|
74
|
|
|
$searchResults[$i]['offer']['price'] = $this->getPageMapProperty($item, 'offer', 'price'); |
75
|
|
|
$searchResults[$i]['offer']['pricecurrency'] = $this->getPageMapProperty($item, 'offer', 'pricecurrency'); |
76
|
|
|
|
77
|
|
|
++$i; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $searchResults; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get a the value Pagemap property with the given name of the given type of the given item. |
86
|
|
|
* |
87
|
|
|
* @param $item |
88
|
|
|
* @param $type |
89
|
|
|
* @param $attribute |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getPageMapProperty($item, $type, $attribute) |
94
|
|
|
{ |
95
|
|
|
$propertyArray = $item->PageMap->xpath('DataObject[@type="'.$type.'"]/Attribute[@name="'.$attribute.'"]/@value'); |
96
|
|
|
|
97
|
|
|
if (! count($propertyArray)) { |
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return (string) $propertyArray[0]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|