Http.php
3.75 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Created by PhpStorm.
* User: liuziyang
* Date: 14-2-28
* Time: 0:47
*/
class YHMCart_Library_Http
{
private $method = 'get';
private $content = '';
private $httpCode = 500;
private $parameter = array();
private $curlInfo = array();
private $debug = false;
protected $curlOpt = array(
'SSL_VERIFYPEER' => false,
'USERAGENT' => "YOHOBuy/2.4.0 CHNetWork/672.1.13 Darwin/13.2.0",
'TIMEOUT' => 60,
'CONNECTTIMEOUT' => 60
);
public function __construct($url, array $opt = array())
{
$this->url = $url;
$this->setCurlOpt($opt);
}
/**
* 设置配置
* @param array $opt
* @return $this
*/
public function setCurlOpt($opt = array())
{
foreach ($opt as $k => $v) {
$this->curlOpt[strtoupper($k)] = $v;
}
return $this;
}
/**
* POST设置参数
* @param array $parameter
* @return $this
*/
public function setParameterPost(array $parameter)
{
$this->parameter = $parameter;
return $this;
}
/**
* @return $this
*/
public function setDebug($debug = false)
{
$this->debug = $debug;
return $this;
}
/**
* 请求
* @param $method
* @return $this
*/
public function request($method = 'GET')
{
$this->method = $method;
$this->exec();
return $this;
}
/**
* 执行请求
*/
private function exec()
{
$url = trim($this->url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $this->curlOpt['USERAGENT']);
switch ($this->method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
if (!empty($this->parameter)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parameter);
}
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
if (!empty($this->curlOpt['HTTP_VERSION'])) {
curl_setopt($ch, CURLOPT_HTTP_VERSION, $url);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->curlOpt['SSL_VERIFYPEER']);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->curlOpt['CONNECTTIMEOUT']);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->curlOpt['TIMEOUT']);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
if (!empty($this->curlOpt['HTTPHEADER'])) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->curlOpt['HTTPHEADER']);
}
$this->content = curl_exec($ch);
$this->curlInfo = curl_getinfo($ch);
curl_close($ch);
$this->httpCode = $this->curlInfo['http_code'];
}
/**
* 获取header长度
* @param $ch
* @param $header
* @return int
*/
public function getHeader($ch, $header)
{
return strlen($header);
}
/**
* 获取URL返回信息
* @param null $opt
* @return array
*/
public function getInfo($opt = null)
{
if ($opt != null && is_string($opt)) {
return $this->curlInfo[$opt];
}
return $this->curlInfo;
}
/**
* 获取状态
* @return int
*/
public function getStatus()
{
return $this->httpCode;
}
/**
* 获取返回结果
* @return string
*/
public function getBody()
{
return $this->content;
}
}