forked from hackerone/curl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurl.php
More file actions
executable file
·118 lines (97 loc) · 3.47 KB
/
Curl.php
File metadata and controls
executable file
·118 lines (97 loc) · 3.47 KB
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
<?php
/**
* Curl wrapper for Yii
* v - 1.2
* @author hackerone
*/
class Curl extends CComponent {
private $_ch;
// config from config.php
public $options;
// default config
private $_config = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:5.0) Gecko/20110619 Firefox/5.0'
);
private function _exec($url) {
$this->setOption(CURLOPT_URL, $url);
$c = curl_exec($this->_ch);
if (!curl_errno($this->_ch))
return $c;
else
throw new CException(curl_error($this->_ch));
}
public function get($url, $params = array()) {
$this->setOption(CURLOPT_HTTPGET, true);
return $this->_exec($this->buildUrl($url, $params));
}
public function post($url, $data = array()) {
$this->setOption(CURLOPT_POST, true);
$this->setOption(CURLOPT_POSTFIELDS, $data);
return $this->_exec($url);
}
public function put($url, $data, $params = array()) {
// write to memory/temp
$f = fopen('php://temp', 'rw+');
fwrite($f, $data);
rewind($f);
$this->setOption(CURLOPT_PUT, true);
$this->setOption(CURLOPT_INFILE, $f);
$this->setOption(CURLOPT_INFILESIZE, strlen($data));
return $this->_exec($this->buildUrl($url, $params));
}
public function buildUrl($url, $data = array()) {
$parsed = parse_url($url);
isset($parsed['query']) ? parse_str($parsed['query'], $parsed['query']) : $parsed['query'] = array();
$params = isset($parsed['query']) ? array_merge($parsed['query'], $data) : $data;
$parsed['query'] = ($params) ? '?' . http_build_query($params) : '';
if (!isset($parsed['path']))
$parsed['path'] = '/';
$port = '';
if(isset($parsed['port'])){
$port = ':' . $parsed['port'];
}
return $parsed['scheme'] . '://' . $parsed['host'] .$port. $parsed['path'] . $parsed['query'];
}
public function setOptions($options = array()) {
curl_setopt_array($this->_ch, $options);
return $this;
}
public function setOption($option, $value) {
curl_setopt($this->_ch, $option, $value);
return $this;
}
// sets header for current request
public function setHeaders($header)
{
if($this->_isAssoc($header)){
$out = array();
foreach($header as $k => $v){
$out[] = $k .': '.$v;
}
$header = $out;
}
$this->setOption(CURLOPT_HTTPHEADER, $header);
return $this;
}
// initialize curl
public function init() {
try {
$this->_ch = curl_init();
$options = is_array($this->options) ? ($this->options + $this->_config) : $this->_config;
$this->setOptions($options);
$ch = $this->_ch;
// close curl on exit
Yii::app()->onEndRequest = function() use(&$ch) {
curl_close($ch);
};
} catch (Exception $e) {
throw new CException('Curl not installed');
}
}
}