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

Commit

Permalink
use Guzzle 4 to perform request to google
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jun 23, 2014
1 parent 2745abd commit c1ed891
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions spec/Spatie/GoogleSearch/GoogleSearchSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') );

}
}
36 changes: 20 additions & 16 deletions src/Spatie/GoogleSearch/GoogleSearch.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php namespace Spatie\GoogleSearch;

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

class GoogleSearch implements GoogleSearchInterface {
protected $searchEngineId;
protected $query;

public function __construct($searchEngineId) {
$this->searchEngineId = $searchEngineId;
$this->searchEngineId = $searchEngineId;
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit c1ed891

Please sign in to comment.