1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rOpenDev\Google; |
4
|
|
|
|
5
|
|
|
abstract class Search |
6
|
|
|
{ |
7
|
|
|
use CacheTrait; |
8
|
|
|
use ConfSearchTrait; |
9
|
|
|
use SleepTrait; |
10
|
|
|
|
11
|
|
|
// ======= |
12
|
|
|
// ------- |
13
|
|
|
// ======= |
14
|
|
|
|
15
|
|
|
/** @var int Current page * */ |
16
|
|
|
protected $page = 1; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string contain the current error |
20
|
|
|
*/ |
21
|
|
|
protected $error; |
22
|
|
|
|
23
|
|
|
protected $errors = [ |
24
|
|
|
1 => 'Google Captcha', |
25
|
|
|
2 => 'Google `We\'re sorry` (flagged as automated request)', |
26
|
|
|
3 => 'Erreurs cURL', |
27
|
|
|
4 => 'Google is redirecting us indefinitely via meta refresh.', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Contient les erreurs provenant du cURL. |
32
|
|
|
*/ |
33
|
|
|
public $cErrors; |
34
|
|
|
|
35
|
|
|
public function generateGoogleSearchUrl() |
36
|
|
|
{ |
37
|
|
|
$this->setParameter('q', $this->q); |
38
|
|
|
$url = 'https://www.google.'.$this->tld.'/search?'.$this->generateParameters(); |
39
|
|
|
|
40
|
|
|
return $url; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function generateParameters() |
44
|
|
|
{ |
45
|
|
|
return http_build_query($this->parameters, '', '&'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string|false Contenu html de la page |
50
|
|
|
*/ |
51
|
|
|
abstract protected function requestGoogle(string $url); |
52
|
|
|
|
53
|
|
|
/* |
54
|
|
|
* Am I Kicked By Google ? Did you reach the google limits ?! |
55
|
|
|
* |
56
|
|
|
* @param string $output Html source |
57
|
|
|
* |
58
|
|
|
* @return int|false |
59
|
|
|
*/ |
60
|
|
|
protected function amIKickedByGoogleThePowerful($output) |
61
|
|
|
{ |
62
|
|
|
/* Google respond : |
63
|
|
|
* We're sorry...... but your computer or network may be sending automated queries. |
64
|
|
|
* To protect our users, we can't process your request right now.' |
65
|
|
|
*/ |
66
|
|
|
if (false !== strpos($output, '<title>Sorry...</title>')) { |
67
|
|
|
return 2; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/* Captcha Google */ |
71
|
|
|
elseif (false !== strpos($output, 'document.getElementById(\'captcha')) { |
72
|
|
|
return 1; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/* RAS */ |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string explaining the error |
81
|
|
|
*/ |
82
|
|
|
public function getError() |
83
|
|
|
{ |
84
|
|
|
if (null !== $this->error) { |
85
|
|
|
return $this->errors[$this->error]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array|false containing the results with column type, link, title |
93
|
|
|
*/ |
94
|
|
|
public function extractResults() |
95
|
|
|
{ |
96
|
|
|
for ($this->page = 1; $this->page <= $this->nbrPage; ++$this->page) { |
97
|
|
|
if (! isset($url)) {// On génère l'url pour la première requète... Ensuite, on utilisera le lien Suivant. |
98
|
|
|
$url = $this->generateGoogleSearchUrl(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$output = $this->requestGoogle($url); |
102
|
|
|
//file_put_contents('debug.html', $output); |
103
|
|
|
if (false === $output) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$extract = new ExtractResults($output); |
108
|
|
|
|
109
|
|
|
$this->result = array_merge($this->result, $extract->getOrganicResults()); |
110
|
|
|
|
111
|
|
|
//h3 > a[href] |
112
|
|
|
if ($this->nbrPage > 1 && $extract->getNextPageLink()) { |
113
|
|
|
$this->execHalfSleep(); |
114
|
|
|
$url = 'https://www.google.'.$this->tld.str_replace('&', '&', $extract->getNextPageLink()); |
115
|
|
|
} else { |
116
|
|
|
break; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* getNbrResults va chercher le nombre de résulats que Google affiche proposer. |
125
|
|
|
* |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
|
|
public function getNbrResults() |
129
|
|
|
{ |
130
|
|
|
$url = $this->generateGoogleSearchUrl(); |
131
|
|
|
$output = $this->requestGoogle($url); |
132
|
|
|
if (false !== $output) { |
133
|
|
|
$html = new \simple_html_dom(); |
134
|
|
|
$html->load($output); |
135
|
|
|
|
136
|
|
|
$rS = $html->find('#resultStats'); |
137
|
|
|
if (isset($rS[0]->plaintext)) { |
138
|
|
|
$s = (string) $this->normalizeTextFromGoogle($rS[0]->plaintext); |
|
|
|
|
139
|
|
|
|
140
|
|
|
return (int) (preg_replace('/[^0-9]/', '', $s)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|