forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Http.php
75 lines (74 loc) · 2.53 KB
/
Http.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
<?php
/**
* HTTP请求类
* @author xiaopengzhu <[email protected]>
* @version 2.0 2012-04-20
*/
class Http
{
/**
* 发起一个HTTP/HTTPS的请求
* @param $url 接口的URL
* @param $params 接口参数 array('content'=>'test', 'format'=>'json');
* @param $method 请求类型 GET|POST
* @param $multi 图片信息
* @param $extheaders 扩展的包头信息
* @return string
*/
public static function request( $url , $params = array(), $method = 'GET' , $multi = false, $extheaders = array())
{
if(!function_exists('curl_init')) exit('Need to open the curl extension');
$method = strtoupper($method);
$ci = curl_init();
curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
$timeout = $multi?30:3;
curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ci, CURLOPT_HEADER, false);
$headers = (array)$extheaders;
switch ($method)
{
case 'POST':
curl_setopt($ci, CURLOPT_POST, TRUE);
if (!empty($params))
{
if($multi)
{
foreach($multi as $key => $file)
{
$params[$key] = '@' . $file;
}
curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
$headers[] = 'Expect: ';
}
else
{
curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
}
}
break;
case 'DELETE':
case 'GET':
$method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
if (!empty($params))
{
$url = $url . (strpos($url, '?') ? '&' : '?')
. (is_array($params) ? http_build_query($params) : $params);
}
break;
}
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
curl_setopt($ci, CURLOPT_URL, $url);
if($headers)
{
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
}
$response = curl_exec($ci);
curl_close ($ci);
return $response;
}
}
?>