Skip to content

Commit

Permalink
fixed compatibility issues with guzzle 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Athlon1600 committed Nov 30, 2016
1 parent abb71d5 commit dc6cb16
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["google scraper", "bing scraper", "google search", "bing search", "google captcha solver", "google proxy"],
"license": "MIT",
"require": {
"guzzlehttp/guzzle": "~5.0"
"guzzlehttp/guzzle": "~6.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 1 addition & 6 deletions src/Engine/GoogleSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ class GoogleSearch extends SearchEngine {
function __construct(){
parent::__construct();

// sometimes google routes the connection through IPv6 which just makes this more difficult to deal with - force it to always use IPv4
$this->client->setDefaultOption('config/curl', array(
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
));

$this->preferences['results_per_page'] = 100;
$this->preferences['google_domain'] = 'google.com';
}
Expand Down Expand Up @@ -78,7 +73,7 @@ private function prepare_url($query, $page){
'oe' => 'utf-8'
);

$vars = array_merge($vars, (array)$preferences['query_params']);
$vars = @array_merge($vars, (array)$this->preferences['query_params']);

if(isset($this->preferences['date_range'])){

Expand Down
49 changes: 28 additions & 21 deletions src/Engine/SearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,34 @@ abstract class SearchEngine {
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"
);

function __construct(){
// default request options to be used with each client request
protected $default_options = array();

// init guzzle client!
$this->client = new Client();
function __construct(){

// request options
$options = array();

// user-agent will be set by setProfileID
$options['headers'] = array(
$this->default_options['headers'] = array(
'Accept' => '*/*',
'Accept-Encoding' => 'gzip, deflate',
'Connection' => 'Keep-Alive'
);

// init options
foreach($options as $key => $val){
$this->client->setDefaultOption($key, $val);
}

// let's put some timeouts in case of slow proxies
$this->client->setDefaultOption('config/curl', array(
$this->default_options['curl'] = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 15
));
CURLOPT_TIMEOUT => 15,
// sometimes google routes the connection through IPv6 which just makes this more difficult to deal with - force it to always use IPv4
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
);

// get_current_user()
// where should we store the cookies for this search client instance? get_current_user()
$this->setCookieDir(sys_get_temp_dir());

// this will create an empty cookie
// this will create an empty cookie profile
$this->setProfileID('default');

// init guzzle client!
$this->reloadClient();
}

public abstract function search($query, $page_num);
Expand All @@ -74,19 +71,26 @@ public function setPreference($name, $value){
$this->preferences[$name] = $value;
}

final private function reloadClient(){
$this->client = new Client($this->default_options);
}

// proxy must be in username:password@IP:Port format
final public function setProxy($proxy, $new_profile = true){

$this->client->setDefaultOption('proxy', $proxy);
$this->default_options['proxy'] = $proxy;

// do we want to use a different cookie profile for this proxy?
if($new_profile){
$this->setProfileID($proxy);
}

$this->reloadClient();
}

final public function disableProxy(){
$this->client->setDefaultOption('proxy', false);
$this->default_options['proxy'] = false;
$this->reloadClient();
}

final public function setCookieDir($cookie_dir){
Expand Down Expand Up @@ -120,7 +124,7 @@ final public function setProfileID($id){

// set it
$agent = $this->agents[$rand_index];
$this->client->setDefaultOption('headers/User-Agent', $agent);
$this->default_options['headers']['User-Agent'] = $agent;

// generate cookie file based on profile_id
$cookie_file = $this->cookie_dir.'/'.$this->cookie_prefix.$this->profile_id.'.json';
Expand All @@ -130,7 +134,10 @@ final public function setProfileID($id){
// cookies will be stored here
$jar = new FileCookieJar($cookie_file);

$this->client->setDefaultOption('cookies', $jar);
//$this->client->setDefaultOption('cookies', $jar);
$this->default_options['cookies'] = $jar;

$this->reloadClient();
}
}

Expand Down

0 comments on commit dc6cb16

Please sign in to comment.