Skip to content

Commit ccacfcd

Browse files
author
zng
committed
使用curl请求http
1 parent 88d782d commit ccacfcd

File tree

4 files changed

+106
-32
lines changed

4 files changed

+106
-32
lines changed

Seefan/Http.php

Lines changed: 93 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Http
1717
* @var array 公用的URL参数
1818
*/
1919
public $query = array();
20+
public $http_prefix = 'http://';
2021

2122
/**
2223
* http的语法
@@ -28,34 +29,103 @@ class Http
2829
*
2930
* @return string
3031
*/
31-
public function request($url, $param = '', $method = 'GET', $header = array())
32+
public function request($url, array $param = array(), $method = 'GET', $header = array())
3233
{
33-
$url = 'http://' . $url;
34-
$get = $this->query;
35-
if (strcasecmp($method, 'get') === 0 && is_array($param)) {
36-
$get = array_merge($get, $param);
37-
$param = '';
34+
$query = array();
35+
$header = array_merge($header, $this->headers);
36+
$body = '';
37+
if (!empty($param['__body'])) {
38+
$body = $param['__body'];
39+
unset($param['__body']);
40+
}
41+
if (strcasecmp($method, 'get') === 0) {
42+
$query = array_merge($this->query, $param);
43+
} else {
44+
if (empty($body)) {
45+
$body = http_build_query($param);
46+
$query = array_merge($this->query);
47+
} else {
48+
$query = array_merge($this->query, $param);
49+
}
50+
}
51+
if (strpos($url, '?') === false) {
52+
$url .= '?' . http_build_query($query);
53+
} else {
54+
$url .= '&' . http_build_query($query);
55+
}
56+
57+
$ch = curl_init();
58+
if ($this->http_prefix == 'https://') {
59+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
60+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
61+
}
62+
curl_setopt($ch, CURLOPT_URL, $this->http_prefix . $url);
63+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
64+
curl_setopt($ch, CURLOPT_HEADER, false);
65+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
66+
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
67+
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
68+
$content = curl_exec($ch);
69+
$status = curl_errno($ch);
70+
if ($status === 0) {
71+
return $content;
72+
} else {
73+
return false;
74+
}
75+
}
76+
77+
public function header($url, array $param = array(), $method = 'GET', $header = array())
78+
{
79+
$query = array();
80+
$header = array_merge($header, $this->headers);
81+
$body = '';
82+
if (!empty($param['__body'])) {
83+
$body = $param['__body'];
84+
unset($param['__body']);
85+
}
86+
if (strcasecmp($method, 'get') === 0) {
87+
$query = array_merge($this->query, $param);
88+
} else {
89+
if (empty($body)) {
90+
$body = http_build_query($param);
91+
$query = array_merge($this->query);
92+
} else {
93+
$query = array_merge($this->query, $param);
94+
}
3895
}
3996
if (strpos($url, '?') === false) {
40-
$url .= '?' . http_build_query($get);
97+
$url .= '?' . http_build_query($query);
4198
} else {
42-
$url .= '&' . http_build_query($get);
43-
}
44-
if (is_array($param)) {
45-
$param = http_build_query($param);
46-
}
47-
$http['method'] = $method;
48-
$http['header'] = '';
49-
$header['Content-Length'] = strlen($param);
50-
$header = array_replace($this->headers, $header);
51-
foreach ($header as $k => $v) {
52-
if (!empty($v)) {
53-
$http['header'] .= $k . ':' . $v . "\r\n";
99+
$url .= '&' . http_build_query($query);
100+
}
101+
102+
$ch = curl_init();
103+
if ($this->http_prefix == 'https://') {
104+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
105+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
106+
}
107+
curl_setopt($ch, CURLOPT_URL, $this->http_prefix . $url);
108+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
109+
curl_setopt($ch, CURLOPT_HEADER, true);
110+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
111+
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
112+
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
113+
$content = curl_exec($ch);
114+
$status = curl_errno($ch);
115+
curl_close($ch);
116+
$header = array();
117+
if ($status === 0) {
118+
if (($index = strpos($content, "\r\n\r\n")) !== false) {
119+
$header_body = substr($content, 0, $index);
120+
$header_body = explode("\r\n", $header_body);
121+
foreach ($header_body as $r) {
122+
$index = strpos($r, ':');
123+
if ($index !== false) {
124+
$header[trim(substr($r, 0, $index))] = trim(substr($r, $index + 1));
125+
}
126+
}
54127
}
55128
}
56-
$http['content'] = $param;
57-
$opts = array('http' => $http);
58-
$context = stream_context_create($opts);
59-
return @file_get_contents($url, false, $context);
129+
return $header;
60130
}
61131
}

Seefan/Service/Kv.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ public function delete($key, array $param = array())
2222
return $resp == 'true';
2323
}
2424

25-
public function set($key, $value)
25+
public function set($key, $value, array $param = array())
2626
{
2727
$url = $this->name . '/' . $key;
2828
if (is_array($value) || is_object($value)) {
2929
$value = json_encode($value);
3030
}
31-
$resp = $this->http->request($this->base_url . $url, $value, 'PUT');
31+
$param['__body']=$value;
32+
$resp = $this->http->request($this->base_url . $url, $param, 'PUT');
3233
return $resp == 'true';
3334
}
3435

Seefan/Service/Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __call($method_name, $args)
4646
if (empty($param)) {
4747
$resp = $this->http->request($this->base_url . $url);
4848
} else {
49-
$resp = $this->http->request($this->base_url . $url, json_encode($param), 'PUT');
49+
$resp = $this->http->request($this->base_url . $url, array('__body' => json_encode($param)), 'PUT');
5050
}
5151
}
5252
return $this->response($resp);

test.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//var_dump($cc->kv->get('service/0000'));
1212
//var_dump($cc->agent->self());
1313
//echo $cc->kv->set('test/test1', 'zng');
14-
//$cc->kv->delete('test/test1');
14+
$cc->kv->delete('test/test1');
1515
$str = '{
1616
"ID": "redis1",
1717
"Name": "scene",
@@ -30,10 +30,10 @@
3030
}
3131
}';
3232

33-
//$rsp=$cc->agent->service('deregister','redis1');
33+
$rsp=$cc->agent->service('deregister','redis1');
3434
//$param = json_decode($str, true);
3535
//$rsp = $cc->agent->service('register', $param);
36-
//$rsp=$cc->catalog->service('XE');,
36+
//$rsp=$cc->catalog->service('XE');
3737

3838
//$json_str = '{
3939
// "Datacenter": "dc1",
@@ -77,6 +77,9 @@
7777
//$rsp = $cc->catalog->deregister($json);
7878

7979
//$rsp=$cc->agent->checks();
80-
$param['recurse'] = true;
81-
$rsp = $cc->kv->get('config', $param);
82-
var_dump($rsp);
80+
//$param['recurse'] = true;
81+
//$rsp = $cc->kv->get('config', $param);
82+
//var_dump($rsp);
83+
//$http = new Seefan\Http();
84+
//$r=$http->header('10.20.28.51:8500/v1/kv/config', ['recurse' => true]);
85+
//print_r($r);

0 commit comments

Comments
 (0)