From c1ed891783778a0b9420db4a9e5c31e60c9fa371 Mon Sep 17 00:00:00 2001 From: freek Date: Mon, 23 Jun 2014 12:18:29 +0200 Subject: [PATCH] use Guzzle 4 to perform request to google --- .travis.yml | 1 - composer.json | 5 +-- spec/Spatie/GoogleSearch/GoogleSearchSpec.php | 3 +- src/Spatie/GoogleSearch/GoogleSearch.php | 36 ++++++++++--------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9aec389..cb1dd86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.3 - 5.4 - 5.5 - 5.6 diff --git a/composer.json b/composer.json index 79a7c0a..4c65c72 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,9 @@ } ], "require": { - "php": ">=5.3.0", - "illuminate/support": "~4.1" + "php": ">=5.4.0", + "illuminate/support": "~4.1", + "guzzlehttp/guzzle": "~4.0" }, "require-dev": { "phpspec/phpspec": "2.0.*@dev" diff --git a/spec/Spatie/GoogleSearch/GoogleSearchSpec.php b/spec/Spatie/GoogleSearch/GoogleSearchSpec.php index 8bdf9ca..469f9d0 100644 --- a/spec/Spatie/GoogleSearch/GoogleSearchSpec.php +++ b/spec/Spatie/GoogleSearch/GoogleSearchSpec.php @@ -23,8 +23,7 @@ function it_should_throw_an_exception_when_called_with_an_empty_string() function it_should_throw_an_exception_when_no_valid_engine_id_is_set() { - - $this->shouldThrow(new \Exception("could not get results"))->during('getResults', array('test-query') ); + $this->shouldThrow('\Exception')->during('getResults', array('test-query') ); } } diff --git a/src/Spatie/GoogleSearch/GoogleSearch.php b/src/Spatie/GoogleSearch/GoogleSearch.php index 5e18ae5..6e982de 100755 --- a/src/Spatie/GoogleSearch/GoogleSearch.php +++ b/src/Spatie/GoogleSearch/GoogleSearch.php @@ -1,13 +1,14 @@ searchEngineId = $searchEngineId; + $this->searchEngineId = $searchEngineId; } /** @@ -20,30 +21,33 @@ public function __construct($searchEngineId) { */ public function getResults($query) { - $searchResults = array(); + $searchResults = []; if ($query == '') { return $searchResults; } - $url = "http://www.google.com/cse?cx=" . $this->searchEngineId . "&client=google-csbe&num=20&output=xml_no_dtd&q=" . urlencode($query); + if ($this->searchEngineId == '') { + throw new \Exception('You must specify a searchEngineId'); + } + + $client = new Client(); + $result = $client->get('http://www.google.com/cse', ['query' => + + ['cx'=> $this->searchEngineId, + 'client'=> 'google-csbe', + 'num' => 20, + 'output'=> 'xml_no_dtd', + 'q'=> $query + ] + ]); - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL,$url); - curl_setopt($curl, CURLOPT_FAILONERROR, 1); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);// allow redirects - curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); // return into a variable - curl_setopt($curl, CURLOPT_TIMEOUT, 3); // times out after 4s -//submit the xml request and get the response - $result = curl_exec($curl); - if(! $result) { - throw new \Exception('could not get results'); + if ($result->getStatusCode() != 200) { + throw new \Exception('Resultcode was not ok: ' . $result->getStatusCode()); } - curl_close($curl); -//now parse the xml with - $xml = simplexml_load_string($result); + $xml = simplexml_load_string($result->getBody()); if ($xml->RES->R) { $i=0;