002.
003.
004.
005.
006.
007.
008.
009.
010.
011.
012.
013.
014.
015.
016.
017.
018.
019.
020.
021.
022.
023.
024.
025.
026.
027.
028.
029.
030.
class HttpClient
031.
{
032.
private $error = '';
033.
034.
private $post = array();
035.
036.
private $header = array();
037.
038.
private $method = 'GET';
039.
040.
private $url = '';
041.
042.
private $body = '';
043.
044.
private $args = array();
045.
046.
private $timeout = 30;
047.
048.
private $returnHeader = array();
049.
050.
private $data = array();
051.
052.
public function setTimeout($time)
053.
{
054.
$time = intval($time);
055.
if ($time > 0)
056.
{
057.
$this->timeout = $time;
058.
}
059.
}
060.
061.
062.
063.
064.
065.
066.
public function get($url,$data=array())
067.
{
068.
$this->method = 'GET';
069.
if (!emptyempty($data))
070.
{
071.
$query = $this->getQueryString($data);
072.
if (strpos($url,'?') !== false)
073.
{
074.
$url .= '&'.$query;
075.
}
076.
else
077.
{
078.
$url .= '?'.$query;
079.
}
080.
}
081.
082.
return $this->doSocket($url);
083.
}
084.
085.
086.
087.
088.
089.
090.
091.
092.
public function post($url,$data=array())
093.
{
094.
$this->method = 'POST';
095.
if (!emptyempty($data))
096.
{
097.
$this->data = $this->getQueryData($data);
098.
}
099.
100.
return $this->doSocket($url);
101.
}
102.
103.
104.
105.
106.
107.
108.
private function doSocket($url)
109.
{
110.
$url_array = @parse_url($url);
111.
$port = isset($url_array['port']) ? $url_array['port'] : 80;
112.
$this->args['path'] = isset($url_array['path']) ? $url_array['path'] : '/';
113.
$this->args['host'] = $url_array['host'];
114.
$header = $this->doHeader();
115.
$fp = '';
116.
if (!$fp = @fsockopen($this->args['host'],$port,$errorno,$errstr,$this->timeout))
117.
{
118.
$this->addError('创建Szyocket错误,请检测网址是否正确! ');
119.
return false;
120.
}
121.
122.
if (!fwrite($fp,$header))
123.
{
124.
fclose($fp);
125.
$this->addError('写入头部信息错误');
126.
return false;
127.
}
128.
129.
$data = '';
130.
while (!feof($fp))
131.
{
132.
stream_set_timeout($fp, $this->timeout);
133.
$data .= fgets($fp, 1024);
134.
$info = stream_get_meta_data($fp);
135.
if ($info['timed_out']) {
136.
$this->addError('获取数据超时');
137.
fclose($fp);
138.
return false;
139.
}
140.
}
141.
142.
$pos = strpos($data, "\r\n\r\n");
143.
$head = substr($data, 0, $pos);
144.
$array = explode("\r\n",$head);
145.
146.
if (is_array($array))
147.
{
148.
foreach ($array as $key=>$val)
149.
{
150.
if ($key > 0)
151.
{
152.
list($_head,$_info) = explode(':',$val);
153.
$this->returnHeader[$_head] = trim($_info);
154.
}
155.
else
156.
{
157.
list(,$status,) = explode(' ',$val);
158.
}
159.
160.
}
161.
}
162.
163.
if ($status != 200)
164.
{
165.
$this->addError('请求网址错误,状态码:'.$status);
166.
return false;
167.
}
168.
169.
$this->body = substr($data, $pos + 4, strlen($data) - ($pos + 4));
170.
171.
return $this->body;
172.
}
173.
174.
175.
176.
177.
178.
179.
private function doHeader()
180.
{
181.
$header = $this->method.' '.$this->args['path']." HTTP/1.1\r\n";
182.
$header .= 'Host: '.$this->args['host']."\r\n";
183.
if (!isset($this->header['Accept']))
184.
{
185.
$this->header['Accept'] = '*/*';
186.
}
187.
188.
if (!isset($this->header['User-Agent']))
189.
{
190.
$this->header['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)';
191.
}
192.
193.
if (!isset($this->header['Accept-Language']))
194.
{
195.
$this->header['Accept-Language'] = 'zh-cn';
196.
}
197.
198.
if ($this->method == 'POST')
199.
{
200.
$this->header['Content-type'] = 'application/x-www-form-urlencoded';
201.
$this->header['Content-length'] = strlen($this->data);
202.
}
203.
204.
foreach ($this->header as $key=>$val)
205.
{
206.
$header .= $key.': '.$val."\r\n";
207.
}
208.
209.
$header .= "Connection: Close\r\n\r\n";
210.
if ($this->data)
211.
{
212.
$header .= $this->data."\r\n";
213.
}
214.
$this->data = '';
215.
$this->header = array();
216.
return $header;
217.
}
218.
219.
/**
220.
* 将一个数组组合成GET参数,如: array('name'=>'zmh','id'=>1) 转换为 : name=zmh&id=1
221.
*
222.
* @param array $data
223.
*/
224.
private function getQueryData($data)
225.
{
226.
$querystring = '';
227.
if (is_array($data)) {
228.
foreach ($data as $key => $val)
229.
{
230.
if (is_array($val))
231.
{
232.
foreach ($val as $val2)
233.
{
234.
$querystring .= urlencode($key).'='.urlencode($val2).'&';
235.
}
236.
}
237.
else
238.
{
239.
$querystring .= urlencode($key).'='.urlencode($val).'&';
240.
}
241.
}
242.
$querystring = substr($querystring, 0, -1);
243.
} else {
244.
$querystring = $data;
245.
}
246.
return $querystring;
247.
}
248.
249.
250.
251.
252.
253.
254.
public function getHeader()
255.
{
256.
return $this->returnHeader;
257.
}
258.
259.
260.
261.
262.
263.
264.
265.
266.
267.
public function setHeader($name,$value)
268.
{
269.
$this->header[ucfirst($name)] = $value;
270.
}
271.
272.
273.
274.
275.
276.
277.
private function addError($msg)
278.
{
279.
$this->error = $msg;
280.
}
281.
282.
283.
284.
285.
286.
287.
public function getError()
288.
{
289.
return $this->error;
290.
}
291.
292.
293.
294.
295.
296.
297.
298.
public function getBody()
299.
{
300.
return $this->body;
301.
}
302.
}