-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl.php
77 lines (58 loc) · 1.59 KB
/
curl.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
<?php
/*
* A cURL PHP class which implements the singleton design pattern
*/
class curl {
# private properties
private static $instance = null;
private $curlResource = null;
private $requestsOutputs = array();
# the static method for the singleton
public static function instance() {
if (self::$instance === null) {
self::$instance = new curl();
}
return self::$instance;
}
# the constructor with initial settings
private function __construct() {
$this->setCurlResource(curl_init());
# Default options
$this->setOption(CURLOPT_RETURNTRANSFER, 1);
}
# close a cURL session
public function __destruct() {
curl_close($this->getCurlResource());
}
# set a cURL option
public function setOption($name, $value) {
$curlSetOption = curl_setopt($this->getCurlResource(), $name, $value);
if (!$curlSetOption) {
throw new RuntimeException('setOption: unable to set the cURL option');
}
}
# set a bunch of cURL options passed through array
public function setOptions($options) {
foreach ($options as $key => $value) {
$this->setOption($key, $value);
}
}
# open the connection and get the result
public function startConnection() {
$result = curl_exec($this->getCurlResource());
if ($result === FALSE) {
throw new RuntimeException('startConnection: unable to execute the connection');
} else {
array_push($this->requestsOutputs, $result);
return $result;
}
}
# getter and setter methods
public function setCurlResource($curlResource) {
$this->curlResource = $curlResource;
}
public function getCurlResource() {
return $this->curlResource;
}
}
?>