|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chay22\RecSelMeter; |
|
4
|
|
|
|
|
5
|
|
|
abstract class Connection |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* URL to execute |
|
9
|
|
|
* |
|
10
|
|
|
* @var $url |
|
11
|
|
|
*/ |
|
12
|
|
|
public $url; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Contains curl_init() if success |
|
16
|
|
|
* |
|
17
|
|
|
* @var $init |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $init = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Additional cURL options |
|
23
|
|
|
* |
|
24
|
|
|
* @var $options |
|
25
|
|
|
*/ |
|
26
|
|
|
public $options = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var $header |
|
30
|
|
|
*/ |
|
31
|
|
|
public $header = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var $content |
|
35
|
|
|
*/ |
|
36
|
|
|
public $content; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $url Store URL |
|
40
|
|
|
*/ |
|
41
|
1 |
|
function __construct($url) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
1 |
|
if (!function_exists('curl_version')) { |
|
44
|
|
|
throw new \Exception('This library needs cURL to run!'); |
|
45
|
|
|
} |
|
46
|
1 |
|
$this->init = $this->init($url); |
|
|
|
|
|
|
47
|
1 |
|
$this->connection(); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set additional curl_setopt_array() options |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $options cURL Options |
|
54
|
|
|
* @see http://php.net/manual/en/function.curl-setopt.php |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setOptions($options = []) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->options = $options; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Running the curl session |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function connection() |
|
68
|
|
|
{ |
|
69
|
|
|
|
|
70
|
1 |
|
$ch = $this->init; |
|
71
|
1 |
|
curl_setopt_array($ch, ($this->options() + $this->options)); |
|
72
|
1 |
|
if (!$this->content = curl_exec($ch)){ |
|
73
|
|
|
throw new \Exception( |
|
74
|
|
|
'CURL ERROR! Code: ' . curl_errno($ch) . |
|
75
|
|
|
' Message: ' . curl_error($ch) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
1 |
|
$this->header = curl_getinfo($ch); |
|
|
|
|
|
|
79
|
1 |
|
curl_close($ch); |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get result of both header and content from curl |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function result() |
|
88
|
|
|
{ |
|
89
|
|
|
return [ |
|
90
|
|
|
'headers', $this->connection['headers'], |
|
|
|
|
|
|
91
|
|
|
'header', $this->connection['header'], |
|
92
|
|
|
'content', $this->connection['content'], |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set cURL options |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
1 |
|
protected function options() |
|
102
|
|
|
{ |
|
103
|
|
|
return [ |
|
104
|
1 |
|
CURLOPT_RETURNTRANSFER => true, |
|
105
|
1 |
|
CURLOPT_FOLLOWLOCATION => true, |
|
106
|
1 |
|
CURLOPT_ENCODING => '', |
|
107
|
1 |
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
108
|
1 |
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Validate URL and start cURL session; |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $url Store URL |
|
115
|
|
|
*/ |
|
116
|
1 |
|
protected function init($url) |
|
117
|
|
|
{ |
|
118
|
|
|
//Grab url id in case $url is not valid as required url |
|
119
|
1 |
|
$id = $url; |
|
120
|
1 |
|
if (strpos($url, 'product') !== false) { |
|
121
|
1 |
|
$id = explode('product',$url); |
|
122
|
1 |
|
$id = end($id); |
|
123
|
1 |
|
$id = explode('/', $id)[1]; |
|
124
|
1 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
//Set the $id as required url |
|
127
|
1 |
|
$this->url = 'http://fjb.kaskus.co.id/product/' . $id; |
|
128
|
1 |
|
if (!curl_init($url)) { |
|
129
|
|
|
throw new \Exception('Can\'t start cURL session.'); |
|
130
|
|
|
} |
|
131
|
1 |
|
return curl_init($url); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.