-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurlConnect.php
145 lines (129 loc) · 3.46 KB
/
CurlConnect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Wrapper curl_exec() for useing the original more easier.
* (POST or GET)
*/
class CurlConnect {
/**
* keys of result that $this->exec() returns
*/
const CH_RETRIEVE_KEY_RESPONSE = 'response';
const CH_RETRIEVE_KEY_INFO = 'info';
const CH_RETRIEVE_KEY_ERROR = 'error';
/**
* cURL handle
*
* @var cURL handle
*/
private $ch;
/**
* tmp file for cookie
*
* @var string
*/
// private $tmpFile;
/**
* curl options
*
* @var array
*/
private $options;
/**
* constructor
*
* @param array $options curl options (optional)
* @return void
*/
public function __construct($options = [])
{
$this->options = $options;
}
/**
* destructor
*
* @return void
*/
public function __destruct() {
// unlink($this->tmpfile);
curl_close($this->ch);
unset($this->ch);
}
/**
* Get result using POST method.
*
* @param string $url URL connect to
* @param array $postFields post parameters
* @return array
*/
public function viaPost($url, $postFields)
{
$options[CURLOPT_CUSTOMREQUEST] = 'POST';
$options[CURLOPT_POSTFIELDS] = http_build_query($postFields);
return $this->exec($url, $options);
}
/**
* Get result using GET method.
*
* @param string $url URL connect to
* @return array
*/
public function viaGet($url)
{
$options[CURLOPT_CUSTOMREQUEST] = 'GET';
return $this->exec($url, $options);
}
/**
* Execute and get the result.
*
* @param string $url URL connect to
* @param array $options parameters to connect
* @return array
*/
private function exec($url, $options)
{
if (!isset($this->ch)) {
$this->ch = curl_init();
}
// @todo: uncomment if cookie was needed.
// if (!isset($this->tmpfile)) {
// $this->tmpfile = tempnam(sys_get_temp_dir(), 'tmp');
// }
curl_reset($this->ch);
$options[CURLOPT_URL] = $url;
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = 60;
// $options[CURLOPT_COOKIEFILE] = $this->tmpfile;
// $options[CURLOPT_COOKIEJAR] = $this->tmpfile;
curl_setopt_array($this->ch, ($this->options + $options));
$response = curl_exec($this->ch);
$info = curl_getinfo($this->ch);
$error = curl_error($this->ch);
if ($error) {
throw new \Exception($error);
}
$result = [
self::CH_RETRIEVE_KEY_RESPONSE => $response,
self::CH_RETRIEVE_KEY_INFO => $info,
self::CH_RETRIEVE_KEY_ERROR => $error,
];
return $result;
}
}
// How to use.
// try {
// $options = [
// CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0'
// ];
// $curlConnect = new CurlConnect($options);
// $result = $curlConnect->viaGet('https://api.github.com/');
// $json = json_decode($result[CurlConnect::CH_RETRIEVE_KEY_RESPONSE], true);
// pretty_print_r($json);
// } catch (Exception $e) {
// pretty_print_r($e);
// }
//
// function pretty_print_r($var) {
// echo '<pre>';
// print_r($var);
// echo '</pre>';
// }