|
1
|
|
|
<?php |
|
2
|
|
|
namespace CViniciusSDias\GoogleCrawler\Proxy; |
|
3
|
|
|
|
|
4
|
|
|
use CViniciusSDias\GoogleCrawler\Exception\InvalidResultException; |
|
5
|
|
|
use CViniciusSDias\GoogleCrawler\Exception\InvalidUrlException; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class that can be used for multiple proxies services, including hideproxy.me, onlinecollege.info, zend2, etc. |
|
11
|
|
|
* |
|
12
|
|
|
* @package CViniciusSDias\GoogleCrawler\Proxy |
|
13
|
|
|
* @author Vinicius Dias |
|
14
|
|
|
*/ |
|
15
|
|
|
class CommonProxy implements GoogleProxyInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var string $endpoint */ |
|
18
|
|
|
protected $endpoint; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor that initializes the specific proxy service |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $endpoint Specific service URL |
|
24
|
|
|
* @throws InvalidUrlException |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function __construct(string $endpoint) |
|
27
|
|
|
{ |
|
28
|
2 |
|
if (!filter_var($endpoint, FILTER_VALIDATE_URL)) { |
|
29
|
|
|
throw new InvalidUrlException("Invalid CommonProxy endpoint: $endpoint"); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
2 |
|
$this->endpoint = $endpoint; |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** {@inheritdoc} */ |
|
36
|
2 |
|
public function getHttpResponse(string $url): ResponseInterface |
|
37
|
|
|
{ |
|
38
|
2 |
|
$data = ['u' => $url, 'allowCookies' => 'on']; |
|
39
|
2 |
|
$httpClient = new Client(['cookies' => true, 'verify' => false]); |
|
40
|
2 |
|
$response = $httpClient->request('POST', $this->endpoint, ['form_params' => $data]); |
|
41
|
|
|
|
|
42
|
2 |
|
return $response; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** {@inheritdoc} */ |
|
46
|
1 |
|
public function parseUrl(string $url): string |
|
47
|
|
|
{ |
|
48
|
1 |
|
$link = parse_url($url); |
|
49
|
1 |
|
parse_str($link['query'], $link); |
|
50
|
|
|
|
|
51
|
1 |
|
parse_str($link['u'], $link); |
|
52
|
1 |
|
$link = array_values($link); |
|
53
|
|
|
|
|
54
|
1 |
|
$url = filter_var($link[0], FILTER_VALIDATE_URL); |
|
55
|
|
|
// If this is not a valid URL, so the result is (probably) an image, news or video suggestion |
|
56
|
1 |
|
if (!$url) { |
|
57
|
|
|
throw new InvalidResultException(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return $url; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|