1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pilipinews\Website\Sunstar; |
4
|
|
|
|
5
|
|
|
use Nacmartin\PhpExecJs\PhpExecJs as Executor; |
6
|
|
|
use Nacmartin\PhpExecJs\Runtime\ExternalRuntime; |
7
|
|
|
use Pilipinews\Common\Client as CurlClient; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Sunstar cURL Client |
11
|
|
|
* |
12
|
|
|
* @package Pilipinews |
13
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Client extends CurlClient |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Nacmartin\PhpExecJs\PhpExecJs |
19
|
|
|
*/ |
20
|
|
|
protected $executor; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initializes the cURL session. |
24
|
|
|
*/ |
25
|
33 |
|
public function __construct() |
26
|
|
|
{ |
27
|
33 |
|
$binaries = array('node', 'nodejs'); |
28
|
|
|
|
29
|
33 |
|
$runtime = new ExternalRuntime(null, $binaries); |
30
|
|
|
|
31
|
33 |
|
parent::__construct(); |
32
|
|
|
|
33
|
33 |
|
$this->executor = new Executor($runtime); |
34
|
33 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Performs the HTTP request based on the given URL. |
38
|
|
|
* |
39
|
|
|
* @param string $url |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
33 |
|
public static function request($url) |
43
|
|
|
{ |
44
|
33 |
|
$self = new static; |
45
|
|
|
|
46
|
33 |
|
$cert = __DIR__ . '/../cacert.pem'; |
47
|
|
|
|
48
|
33 |
|
$self->set(CURLOPT_SSL_VERIFYPEER, 1); |
49
|
|
|
|
50
|
33 |
|
$self->set(CURLOPT_CAINFO, $cert); |
51
|
|
|
|
52
|
33 |
|
$self->url($url); |
53
|
|
|
|
54
|
33 |
|
$result = $self->execute(false); |
55
|
|
|
|
56
|
33 |
|
if ($result === false) { |
57
|
33 |
|
echo curl_error($self->curl) . PHP_EOL; |
58
|
33 |
|
} else { |
59
|
|
|
echo json_encode($result) . PHP_EOL; |
60
|
|
|
} |
61
|
|
|
|
62
|
33 |
|
if ($self->redirected($result)) { |
63
|
|
|
$pattern = '/<script>(.*?)<\/script>/i'; |
64
|
|
|
|
65
|
|
|
preg_match($pattern, $result, $matches); |
66
|
|
|
|
67
|
|
|
echo json_encode($matches) . PHP_EOL; |
68
|
|
|
|
69
|
|
|
$cookie = $self->cookie($matches[1]); |
70
|
|
|
|
71
|
|
|
$self->set(CURLOPT_COOKIE, $cookie); |
72
|
|
|
} |
73
|
|
|
|
74
|
33 |
|
return $self->execute(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the cookie value based on given script. |
79
|
|
|
* |
80
|
|
|
* @param string $result |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function cookie($result) |
84
|
|
|
{ |
85
|
|
|
$script = str_replace('e(r);', 'r', $result); |
86
|
|
|
|
87
|
|
|
$eval = $this->executor->evalJs($script); |
88
|
|
|
|
89
|
|
|
$search = array('document.cookie=', 'location.reload()'); |
90
|
|
|
|
91
|
|
|
$script = str_replace($search, array('x=', 'x'), $eval); |
92
|
|
|
|
93
|
|
|
return $this->executor->evalJs($script); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Checks if the result is being redirected. |
98
|
|
|
* |
99
|
|
|
* @param string $result |
100
|
|
|
* @return boolean |
101
|
|
|
*/ |
102
|
33 |
|
protected function redirected($result) |
103
|
|
|
{ |
104
|
33 |
|
return strpos($result, 'You are being redirected') !== false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|