1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pilipinews\Website\Sunstar; |
4
|
|
|
|
5
|
|
|
use Nacmartin\PhpExecJs\PhpExecJs; |
6
|
|
|
use Nacmartin\PhpExecJs\Runtime\ExternalRuntime; |
7
|
|
|
|
8
|
|
|
class Client |
9
|
|
|
{ |
10
|
|
|
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var \Nacmartin\PhpExecJs\PhpExecJs |
14
|
|
|
*/ |
15
|
|
|
protected $executor; |
16
|
|
|
|
17
|
33 |
|
public function __construct() |
18
|
|
|
{ |
19
|
33 |
|
$binaries = array('node', 'nodejs'); |
20
|
|
|
|
21
|
33 |
|
$runtime = new ExternalRuntime(null, $binaries); |
22
|
|
|
|
23
|
33 |
|
$this->executor = new PhpExecJs($runtime); |
24
|
33 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Performs the HTTP request based on the given URL. |
28
|
|
|
* |
29
|
|
|
* @param string $link |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
33 |
|
public static function request($link) |
33
|
|
|
{ |
34
|
33 |
|
$self = new static; |
35
|
|
|
|
36
|
33 |
|
$result = $self->execute($link); |
37
|
|
|
|
38
|
33 |
|
$result = preg_match('/<script>(.*?)<\/script>/i', $result, $matches); |
39
|
|
|
|
40
|
33 |
|
echo $result . PHP_EOL; |
41
|
|
|
|
42
|
33 |
|
echo json_encode($matches) . PHP_EOL; |
43
|
|
|
|
44
|
33 |
|
$script = str_replace('e(r);', 'r', $matches[1]); |
45
|
|
|
|
46
|
|
|
$result = $self->executor->evalJs((string) $script); |
47
|
|
|
|
48
|
|
|
$result = str_replace('document.cookie=', 'x=', $result); |
49
|
|
|
|
50
|
|
|
$script = str_replace('location.reload()', 'x', $result); |
51
|
|
|
|
52
|
|
|
$cookie = $self->executor->evalJs($script); |
53
|
|
|
|
54
|
|
|
return $self->execute($link, (string) $cookie); |
55
|
|
|
} |
56
|
|
|
|
57
|
33 |
|
protected function execute($link, $cookie = null) |
58
|
|
|
{ |
59
|
33 |
|
$curl = curl_init(); |
60
|
|
|
|
61
|
33 |
|
if ($cookie !== null) { |
62
|
|
|
curl_setopt($curl, CURLOPT_COOKIE, $cookie); |
63
|
|
|
} |
64
|
|
|
|
65
|
33 |
|
curl_setopt($curl, CURLOPT_URL, (string) $link); |
66
|
|
|
|
67
|
33 |
|
curl_setopt($curl, CURLOPT_ENCODING, ''); |
68
|
|
|
|
69
|
33 |
|
curl_setopt($curl, CURLOPT_USERAGENT, self::USER_AGENT); |
70
|
|
|
|
71
|
33 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
72
|
|
|
|
73
|
33 |
|
($response = curl_exec($curl)) && curl_close($curl); |
74
|
|
|
|
75
|
33 |
|
return (string) $response; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|