|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rOpenDev\Google; |
|
4
|
|
|
|
|
5
|
|
|
class ExtractResults |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var array from \rOpenDev\Google\ResultTypes.php |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $types; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var object dom |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $html; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(string $source) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->html = new \simple_html_dom(); |
|
20
|
|
|
$this->html->load($source); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function getOrganicResults() |
|
24
|
|
|
{ |
|
25
|
|
|
$results = $this->html->find(str_replace(';', ', ', $this->getSelector('organic'))); |
|
26
|
|
|
$result = []; |
|
27
|
|
|
foreach ($results as $r) { |
|
28
|
|
|
$title = $r->find('h3, [role=heading]', 0); |
|
29
|
|
|
if ($title && $r->find('a', 0)) { |
|
30
|
|
|
$result[] = [ |
|
31
|
|
|
'type' => 'organic', |
|
32
|
|
|
'title' => $this->normalizeTextFromGoogle($title->innertext), |
|
33
|
|
|
'link' => $this->getUrlFromGoogleSerpFromat($r->find('a', 0)->href), |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $result; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string or NULL if next page link not found |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getNextPageLink() |
|
45
|
|
|
{ |
|
46
|
|
|
$nextPageLink = $this->html->find('[aria-label="Page suivante"]'); |
|
47
|
|
|
if (isset($nextPageLink[0])) { |
|
48
|
|
|
return $nextPageLink[0]->href; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function getSelector(string $type) |
|
53
|
|
|
{ |
|
54
|
|
|
if (null === $this->types) { |
|
55
|
|
|
$this->types = ResultsTypes::get(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->types[$type]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected static function getUrlFromGoogleSerpFromat($str) |
|
62
|
|
|
{ |
|
63
|
|
|
preg_match('/\/url\?.*(q|url)=(http.+)&/SiU', $str, $m1); |
|
64
|
|
|
$str = isset($m1[2]) ? $m1[2] : $str; |
|
65
|
|
|
|
|
66
|
|
|
return $str; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function normalizeTextFromGoogle($text) |
|
70
|
|
|
{ |
|
71
|
|
|
return htmlspecialchars_decode(html_entity_decode(strip_tags($text))); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|