|
1
|
|
|
<?php |
|
2
|
|
|
namespace CViniciusSDias\GoogleCrawler\Proxy; |
|
3
|
|
|
|
|
4
|
|
|
use CViniciusSDias\GoogleCrawler\Exception\InvalidResultException; |
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class that for using the kproxy.com servers |
|
10
|
|
|
* |
|
11
|
|
|
* @package CViniciusSDias\GoogleCrawler\Proxy |
|
12
|
|
|
* @author Vinicius Dias |
|
13
|
|
|
*/ |
|
14
|
|
|
class KProxy implements GoogleProxyInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string $endpoint */ |
|
17
|
|
|
protected $endpoint; |
|
18
|
|
|
/** @var int $serverNumber */ |
|
19
|
|
|
protected $serverNumber; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor that initializes the proxy service in one of its servers, which go from 1 to 9 |
|
23
|
|
|
* |
|
24
|
|
|
* @param int $serverNumber |
|
25
|
|
|
*/ |
|
26
|
8 |
|
public function __construct(int $serverNumber) |
|
27
|
|
|
{ |
|
28
|
8 |
|
if ($serverNumber > 9 || $serverNumber < 1) { |
|
29
|
2 |
|
throw new \InvalidArgumentException(); |
|
30
|
|
|
} |
|
31
|
6 |
|
$this->serverNumber = $serverNumber; |
|
32
|
6 |
|
$this->endpoint = "http://server{$serverNumber}.kproxy.com"; |
|
33
|
6 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
* @throws \GuzzleHttp\Exception\ServerException If the proxy was overused |
|
38
|
|
|
* @throws \GuzzleHttp\Exception\ConnectException If the proxy is unavailable |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function getHttpResponse(string $url): ResponseInterface |
|
41
|
|
|
{ |
|
42
|
4 |
|
$httpClient = new Client(['cookies' => true]); |
|
43
|
4 |
|
$this->accessMainPage($httpClient); |
|
44
|
4 |
|
$this->sendRequestToProxy($httpClient, $url); |
|
45
|
|
|
|
|
46
|
4 |
|
$parsedUrl = parse_url($url); |
|
47
|
4 |
|
$queryString = $parsedUrl['query']; |
|
48
|
4 |
|
$actualUrl = "{$this->endpoint}/servlet/redirect.srv/swh/suxm/sqyudex/spqr/p1/search?{$queryString}"; |
|
49
|
|
|
|
|
50
|
4 |
|
return $httpClient->request('GET', $actualUrl); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Accesses the main page of the kproxy.com server. This is mandatory. |
|
55
|
|
|
* |
|
56
|
|
|
* @param Client $httpClient |
|
57
|
|
|
*/ |
|
58
|
4 |
|
private function accessMainPage(Client $httpClient): void |
|
59
|
|
|
{ |
|
60
|
4 |
|
$httpClient->request('GET', "{$this->endpoint}/index.jsp"); |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** {@inheritdoc} */ |
|
64
|
5 |
|
public function parseUrl(string $url): string |
|
65
|
|
|
{ |
|
66
|
5 |
|
$parsedUrl = parse_url($url); |
|
67
|
5 |
|
parse_str($parsedUrl['query'], $link); |
|
68
|
|
|
|
|
69
|
5 |
|
if (!array_key_exists('q', $link)) { |
|
70
|
|
|
// Generally a book suggestion |
|
71
|
|
|
throw new InvalidResultException(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
$url = filter_var($link['q'], FILTER_VALIDATE_URL); |
|
75
|
|
|
// If this is not a valid URL, so the result is (probably) an image, news or video suggestion |
|
76
|
5 |
|
if (!$url) { |
|
77
|
1 |
|
throw new InvalidResultException(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
return $url; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sends the request to the proxy service that saves the info in session. After this we can redirect |
|
85
|
|
|
* the user to the search results |
|
86
|
|
|
* |
|
87
|
|
|
* @param Client $httpClient |
|
88
|
|
|
* @param string $url |
|
89
|
|
|
*/ |
|
90
|
4 |
|
private function sendRequestToProxy(Client $httpClient, string $url): void |
|
91
|
|
|
{ |
|
92
|
4 |
|
$encodedUrl = urlencode($url); |
|
93
|
4 |
|
$postData = ['page' => $encodedUrl, 'x' => 0, 'y' => 0]; |
|
94
|
|
|
$headers = [ |
|
95
|
4 |
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' |
|
96
|
|
|
]; |
|
97
|
4 |
|
$httpClient->request( |
|
98
|
4 |
|
'POST', |
|
99
|
4 |
|
"{$this->endpoint}/doproxy.jsp", |
|
100
|
4 |
|
['form_params' => $postData, 'headers' => $headers] |
|
101
|
|
|
); |
|
102
|
4 |
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|