forked from kranack/noodle-doodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurlExtend.php
148 lines (117 loc) · 4.74 KB
/
CurlExtend.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
146
147
148
<?php
/**
* @author decima
* This class offers access to simplify Curl use
*/
class CurlExtend {
protected function encode_rfc3986($input) {
$output = rawurlencode($input);
$output = str_replace('%7E', '~', $output);
$output = str_replace('&', '&', $output);
$output = str_replace('+', ' ', $output);
return $output;
}
protected function getSignedParams($method, $url, $params, $secret = null) {
$params['oauth_version'] = "1.0";
$params['oauth_signature_method'] = "HMAC-SHA1";
$params['oauth_timestamp'] = time();
$params['oauth_nonce'] = rand();
if (isset($params['oauth_consumer_secret'])) {
$secret = $params['oauth_consumer_secret'];
unset($params['oauth_consumer_secret']);
}
$secret .= "&";
if (isset($params['oauth_token_secret'])) {
$secret .= $params['oauth_token_secret'];
unset($params['oauth_token_secret']);
}
ksort($params);
$enc_params = http_build_query($params);
$enc_url = strtoupper($method) . "&" . $this->encode_rfc3986($url) . "&" . $this->encode_rfc3986($enc_params);
$signature = base64_encode(hash_hmac('sha1', $enc_url, $secret, true));
$params['oauth_signature'] = $signature;
return $params;
}
protected function getCurl($url, $params, $header = array()) {
$params = $this->getSignedParams("get", $url, $params);
$href = $url . "?" . http_build_query($params);
$curl = curl_init($href);
$header[] = "User-Agent: " . $_SERVER['HTTP_USER_AGENT'];
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if ($header != array()) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
$response = curl_exec($curl);
return $response;
}
protected function deleteCurl($url, $params, $header = NULL) {
$params = $this->getSignedParams("delete", $url, $params);
$href = $url . "?" . http_build_query($params);
$curl = curl_init($href);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
$head[] = "Content-type: application/xml";
$head[] = $header;
curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$response = curl_exec($curl);
return $response;
}
protected function postCurl($url, $params, $body, $header = null) {
$params = $this->getSignedParams("post", $url, $params);
$href = $url . "?" . http_build_query($params);
$curl = curl_init($href);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
$head = Array("Content-type: application/xml");
$head[] = $header;
curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);
return $response;
}
protected function putCurl($url, $params, $body, $header = null) {
$params = $this->getSignedParams("put", $url, $params);
$href = $url . "?" . http_build_query($params);
$curl = curl_init($href);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
$head = array("Content-type: application/xml");
$head[] = $header;
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$response = curl_exec($curl);
return $response;
}
protected function parse_get_result($in) {
parse_str($in, $out);
return $out;
}
protected function formate_get_result($in) {
$out = simplexml_load_string($in);
return $out;
}
protected function formate_post_result($in) {
$post = str_replace("\r", "", $in);
$post = explode("\n", $post);
$res = array();
foreach ($post as $p) {
if ($p) {
$arr = explode(": ", $p);
if (count($arr) < 2) {
$res[] = $p;
} else if (count($arr) == 2) {
$res[$arr[0]] = $arr[1];
} else {
$res[] = $p;
}
}
}
return $res;
}
}