Skip to content

Commit a991a09

Browse files
committed
更新OAuth库
1 parent 42aba41 commit a991a09

24 files changed

+1349
-2701
lines changed

server/apps/classes/WeiboOAuth.php

Lines changed: 0 additions & 2701 deletions
This file was deleted.

server/apps/include/libweibo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 2c9524190c4334e208613ad8f65365471c526954
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
<?php
2+
namespace ZenAPI;
3+
4+
class BaseClient {
5+
/**
6+
*
7+
* @var curl Handle
8+
*/
9+
protected $_curlHandle;
10+
11+
/**
12+
* Set up the API root URL.
13+
*
14+
* @ignore
15+
*/
16+
public $host;
17+
18+
/**
19+
*
20+
* @var array
21+
*/
22+
protected $_curlOptions = array(
23+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
24+
CURLOPT_USERAGENT => 'ZenAPI v0.3',
25+
CURLOPT_CONNECTTIMEOUT => 30,
26+
CURLOPT_TIMEOUT => 30,
27+
CURLOPT_SSL_VERIFYPEER => FALSE,
28+
);
29+
30+
/**
31+
* Contains the last HTTP headers returned.
32+
*
33+
* @ignore
34+
*/
35+
public $http_header = array();
36+
37+
/**
38+
* print the debug info
39+
*
40+
* @ignore
41+
*/
42+
public $debug = FALSE;
43+
44+
/**
45+
*
46+
* @param string $host
47+
public function __construct($host = '') {
48+
$this->host = $host;
49+
}
50+
*/
51+
52+
public function setCurlOptions(array $options){
53+
$this->_curlOptions = array_merge($this->_curlOptions, $options);
54+
}
55+
56+
protected function _paramsFilter(&$params){
57+
}
58+
59+
/**
60+
*
61+
* @param string $response
62+
* @throws Exception
63+
* @return array
64+
*/
65+
public function parseResponse($response){
66+
$json = json_decode($response, true);
67+
if ($json === null)
68+
throw new Exception($response);
69+
70+
return $json;
71+
}
72+
73+
/**
74+
* GET shorthand
75+
*
76+
* @return mixed
77+
*/
78+
public function get($url, $parameters = array()) {
79+
$this->_paramsFilter($parameters);
80+
$response = $this->http($this->realUrl($url) . (empty($parameters) ? '' : '?' . http_build_query($parameters)), 'GET', null, $this->_additionalHeaders());
81+
82+
return $this->parseResponse($response);
83+
}
84+
85+
/**
86+
* POST shorthand
87+
*
88+
* @return mixed
89+
*/
90+
public function post($url, $parameters = array()) {
91+
$this->_paramsFilter($parameters);
92+
93+
$body = http_build_query($parameters);
94+
$response = $this->http($this->realUrl($url), 'POST', $body, $this->_additionalHeaders());
95+
96+
return $this->parseResponse($response);
97+
}
98+
99+
public function postMulti($url, $parameters = array()) {
100+
$this->_paramsFilter($parameters);
101+
102+
$boundary = uniqid('------------------');
103+
$body = self::build_http_query_multi($parameters, $boundary);
104+
$headers = array("Content-Type: multipart/form-data; boundary=" . $boundary);
105+
106+
$response = $this->http($this->realUrl($url), 'POST', $body, array_merge($headers, $this->_additionalHeaders()));
107+
108+
return $this->parseResponse($response);
109+
}
110+
111+
/**
112+
* DELTE shorthand
113+
*
114+
* @return mixed
115+
*/
116+
public function delete($url, $parameters = array()) {
117+
$this->_paramsFilter($parameters);
118+
119+
$response = $this->http($this->realUrl($url) . '?' . http_build_query($parameters), 'DELETE');
120+
121+
return $this->parseResponse($response);
122+
}
123+
124+
public function put($url, $parameters = array()) {
125+
$this->_paramsFilter($parameters);
126+
127+
$body = http_build_query($parameters);
128+
$response = $this->http($this->realUrl($url), 'PUT', $body, $this->_additionalHeaders());
129+
130+
return $this->parseResponse($response);
131+
}
132+
133+
public function putMulti($url, $parameters = array()) {
134+
$this->_paramsFilter($parameters);
135+
136+
$boundary = uniqid('------------------');
137+
$body = self::build_http_query_multi($parameters, $boundary);
138+
$headers = array("Content-Type: multipart/form-data; boundary=" . $boundary);
139+
140+
$response = $this->http($this->realUrl($url), 'PUT', $body, array_merge($headers, $this->_additionalHeaders()));
141+
142+
return $this->parseResponse($response);
143+
}
144+
145+
public function patch($url, $parameters = array()) {
146+
$this->_paramsFilter($parameters);
147+
148+
$body = http_build_query($parameters);
149+
$response = $this->http($this->realUrl($url), 'PATCH', $body, $this->_additionalHeaders());
150+
151+
return $this->parseResponse($response);
152+
}
153+
154+
public function patchMulti($url, $parameters = array()) {
155+
$this->_paramsFilter($parameters);
156+
157+
$boundary = uniqid('------------------');
158+
$body = self::build_http_query_multi($parameters, $boundary);
159+
$headers = array("Content-Type: multipart/form-data; boundary=" . $boundary);
160+
161+
$response = $this->http($this->realUrl($url), 'PATCH', $body, array_merge($headers, $this->_additionalHeaders()));
162+
163+
return $this->parseResponse($response);
164+
}
165+
166+
/**
167+
*
168+
* @return array
169+
*/
170+
protected function _additionalHeaders(){
171+
return array();
172+
}
173+
174+
public function realUrl($url){
175+
return $this->host . $url;
176+
}
177+
178+
/**
179+
* Make an HTTP request
180+
*
181+
* @param string $url
182+
* @param string $method
183+
* @param string $postfields
184+
* @param array $headers
185+
* @throws CurlException
186+
* @return string API results
187+
*/
188+
public function http($url, $method, $postfields = NULL, $headers = array()) {
189+
if ($this->_curlHandle !== null){
190+
curl_close($this->_curlHandle);
191+
$this->http_header = array();
192+
}
193+
194+
$this->_curlHandle = $ci = curl_init();
195+
196+
/* Curl settings */
197+
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
198+
curl_setopt($ci, CURLOPT_ENCODING, "");
199+
curl_setopt($ci, CURLOPT_HEADERFUNCTION, function($ch, $header){
200+
$this->http_header[] = $header;
201+
return strlen($header);
202+
});
203+
curl_setopt($ci, CURLOPT_HEADER, FALSE);
204+
curl_setopt($ci, CURLOPT_URL, $url);
205+
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
206+
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
207+
208+
curl_setopt_array($ci, $this->_curlOptions);
209+
210+
switch ($method) {
211+
case 'POST':
212+
curl_setopt($ci, CURLOPT_POST, TRUE);
213+
break;
214+
case 'GET':
215+
curl_setopt($ci, CURLOPT_POST, FALSE);
216+
break;
217+
default:
218+
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method);
219+
}
220+
221+
if (!empty($postfields))
222+
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
223+
224+
$response = curl_exec($ci);
225+
226+
if ($response === false){
227+
throw new CurlException(curl_error($ci), curl_errno($ci));
228+
}
229+
230+
if ($this->debug) {
231+
echo "=====post data======\r\n";
232+
var_dump($postfields);
233+
234+
echo '=====info====='."\r\n";
235+
print_r( curl_getinfo($this->_curlHandle) );
236+
237+
echo '=====$response====='."\r\n";
238+
print_r( $response );
239+
}
240+
return $response;
241+
}
242+
243+
public function __destruct(){
244+
if ($this->_curlHandle !== null){
245+
curl_close($this->_curlHandle);
246+
}
247+
}
248+
249+
/**
250+
*
251+
* @return array
252+
*/
253+
public function getInfo(){
254+
return curl_getinfo($this->_curlHandle);
255+
}
256+
257+
/**
258+
*
259+
* @return string
260+
*/
261+
public function getUrl(){
262+
return curl_getinfo($this->_curlHandle, CURLINFO_EFFECTIVE_URL);
263+
}
264+
265+
/**
266+
* Get the header info
267+
*
268+
* @return array
269+
*/
270+
public function getHeader() {
271+
$header = array();
272+
foreach($this->http_header as $line){
273+
$i = strpos($line, ':');
274+
if (!empty($i)) {
275+
$key = str_replace('-', '_', strtolower(substr($line, 0, $i)));
276+
$value = trim(substr($line, $i + 2));
277+
$header[$key] = $value;
278+
}
279+
}
280+
return $header;
281+
}
282+
283+
/**
284+
*
285+
* @param array $params
286+
* @param string $boundary
287+
* @return string
288+
*/
289+
public static function build_http_query_multi($params, $boundary) {
290+
if (!$params) return '';
291+
292+
//uksort($params, 'strcmp');
293+
294+
$multipartbody = '';
295+
296+
foreach ($params as $parameter => $value) {
297+
if( $value instanceof File) {
298+
$multipartbody .= '--' . $boundary . "\r\n";
299+
$multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $value->filename . '"'. "\r\n";
300+
$multipartbody .= "Content-Type: ' . $value->mime . '\r\n\r\n";
301+
$multipartbody .= $value->content. "\r\n";
302+
}
303+
else {
304+
$multipartbody .= '--' . $boundary . "\r\n";
305+
$multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";
306+
$multipartbody .= $value."\r\n";
307+
}
308+
}
309+
310+
$multipartbody .= '--' . $boundary. '--';
311+
return $multipartbody;
312+
}
313+
}

server/apps/include/zenapi/Client.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace ZenAPI;
3+
4+
class Client extends BaseClient {
5+
/**
6+
*
7+
* @param string $host
8+
*/
9+
public function __construct($host = '') {
10+
$this->host = $host;
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace ZenAPI;
3+
4+
class CurlException extends \Exception{
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace ZenAPI;
3+
4+
class DuoshuoClient extends BaseClient{
5+
/**
6+
*
7+
* @var string
8+
*/
9+
public $access_token;
10+
11+
/**
12+
* Set up the API root URL.
13+
*
14+
* @ignore
15+
*/
16+
public $host = "https://api.duoshuo.com/";
17+
18+
/**
19+
*
20+
* @var string
21+
*/
22+
public $format = 'json';
23+
24+
/**
25+
*
26+
* @param string $access_token
27+
*/
28+
public function __construct($access_token = NULL) {
29+
$this->access_token = $access_token;
30+
}
31+
32+
protected function _paramsFilter(&$params){
33+
$params['access_token'] = $this->access_token;
34+
}
35+
36+
public function realUrl($url){
37+
return $this->host . $url . '.' . $this->format;
38+
}
39+
}

0 commit comments

Comments
 (0)