|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JanDrda\LaravelGoogleCustomSearchEngine; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use JDrda\LaravelGoogleCustomSearchEngine\Interfaces\LaravelGoogleCustomSearchEngineInterface; |
| 7 | + |
| 8 | +class LaravelGoogleCustomSearchEngine implements LaravelGoogleCustomSearchEngineInterface |
| 9 | +{ |
| 10 | + |
| 11 | + /** |
| 12 | + * Custom search engine ID |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $engineId; |
| 17 | + |
| 18 | + /** |
| 19 | + * Google console API key |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $apiKey; |
| 24 | + |
| 25 | + /** |
| 26 | + * Original response converted to array |
| 27 | + * |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + protected $originalResponse; |
| 31 | + |
| 32 | + /** |
| 33 | + * Constructor |
| 34 | + * |
| 35 | + * LaravelGoogleCustomSearchEngine constructor. |
| 36 | + * @param $engineId |
| 37 | + * @param $apiKey |
| 38 | + */ |
| 39 | + public function __construct($engineId, $apiKey) |
| 40 | + { |
| 41 | + $this->engineId = $engineId; |
| 42 | + $this->apiKey = $apiKey; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Setter for engineId, overrides the value from config |
| 47 | + * |
| 48 | + * @param $engineId |
| 49 | + */ |
| 50 | + public function setEngineId($engineId){ |
| 51 | + $this->engineId = $engineId; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Setter for apiKey, overrides the value from config |
| 56 | + * |
| 57 | + * @param $engineId |
| 58 | + */ |
| 59 | + public function setApiKey($apiKey){ |
| 60 | + $this->apiKey = $apiKey; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get search results |
| 65 | + * |
| 66 | + * @param $phrase |
| 67 | + * @return array |
| 68 | + * @throws Exception |
| 69 | + */ |
| 70 | + public function getResults($phrase) |
| 71 | + { |
| 72 | + $searchResults = array(); |
| 73 | + |
| 74 | + if ($phrase == '') { |
| 75 | + return $searchResults; |
| 76 | + } |
| 77 | + |
| 78 | + if ($this->engineId == '') { |
| 79 | + throw new \Exception('You must specify a engineId'); |
| 80 | + } |
| 81 | + |
| 82 | + if ($this->apiKey == '') { |
| 83 | + throw new \Exception('You must specify a apiKey'); |
| 84 | + } |
| 85 | + |
| 86 | + // create a new cURL resource |
| 87 | + $ch = curl_init(); |
| 88 | + |
| 89 | + curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); |
| 90 | + curl_setopt($ch, CURLOPT_HEADER, 0); |
| 91 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
| 92 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 93 | + |
| 94 | + $output = curl_exec($ch); |
| 95 | + |
| 96 | + $info = curl_getinfo($ch); |
| 97 | + |
| 98 | + curl_close($ch); |
| 99 | + |
| 100 | + if ($output === false || $info['http_code'] != 200) { |
| 101 | + |
| 102 | + throw new \Exception("No data returned, code [". $info['http_code']. "] - " . curl_error($ch)); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + return $searchResults; |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments