Skip to content

Commit 922f0ea

Browse files
author
李仕鹏
committed
添加getter and setter
1 parent b6bf5a2 commit 922f0ea

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

CurlHttp.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,45 @@ class CurlHttp extends Component
2121
'User-Agent' => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1',
2222
'Accept-Charset' => 'GBK,utf-8' ,
2323
);
24-
24+
25+
private static $methodDesc = [
26+
self::METHOD_GET => "GET",
27+
self::METHOD_POST => "POST",
28+
self::METHOD_POSTJSON => "POST",
29+
];
30+
2531
// closure
2632
public $beforeRequest;
2733
public $afterRequest;
2834

2935
private $_curl;
36+
private $_action;
3037

3138
public function init()
3239
{
3340
parent::init();
3441
if(empty($this->host)) {
3542
throw new InvalidParamException("Please config host.");
3643
}
37-
3844
}
3945

40-
private function getUrl()
46+
public function getUrl()
4147
{
4248
$url = $this->protocol."://".$this->host;
4349
if($this->port != 80) {
4450
$url .= ":".$this->port;
4551
}
46-
return $url;
52+
return $url.$this->getAction();
53+
}
54+
55+
public function getAction()
56+
{
57+
return $this->_action;
58+
}
59+
60+
public function setAction($action)
61+
{
62+
$this->_action = $action;
4763
}
4864

4965
public function setMethod($method)
@@ -59,7 +75,15 @@ public function setGet()
5975
}
6076
return $this->setMethod(self::METHOD_GET);
6177
}
62-
78+
79+
public function getMethod()
80+
{
81+
if(isset(self::$methodDesc[$this->method])) {
82+
return self::$methodDesc[$this->method];
83+
}
84+
return "GET";
85+
}
86+
6387
public function setPost()
6488
{
6589
if(!empty($this->headers['Content-Type'])) {
@@ -108,19 +132,27 @@ public function getCurl()
108132
public function httpExec($action = "/", $params = array())
109133
{
110134
$ch = $this->getCurl();
135+
$this->setAction($action);
111136
if($this->beforeRequest instanceof Closure) {
112137
$params = call_user_func($this->beforeRequest, $params, $this);
113138
empty($params) && $params = [];
114139
}
115-
$url = $this->getUrl().$action;
140+
$url = $this->getUrl();
116141
if ($this->method == self::METHOD_POST) {
117142
curl_setopt($ch, CURLOPT_POST, 1);
118143
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
119144
} elseif ($this->method == self::METHOD_POSTJSON) {
120145
curl_setopt($ch, CURLOPT_POST, 1);
121146
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
122147
} else {
123-
!empty($params) && $url .= "?".http_build_query($params);
148+
if(!empty($params)) {
149+
$temp = explode("?", $url);
150+
if(count($temp) > 1) {
151+
$url = $temp[0]."?".$temp[1].'&'.http_build_query($params);
152+
} else {
153+
$url = $url."?".http_build_query($params);
154+
}
155+
}
124156
}
125157
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
126158
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);

demo/test.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@
33
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
44
require(__DIR__ . '/../CurlHttp.php');
55
$curl = new \lspbupt\curl\CurlHttp([
6-
'host' => "www.baidu.com",
7-
'beforeRequest' => function($params, $curlhttp) {
8-
$params['q'] = "ldi";
9-
var_dump($curlhttp);
10-
},
11-
'afterRequest' => function($output, $curlhttp) {
12-
var_dump($curlhttp);
6+
'host' => 'apis.baidu.com',
7+
'beforeRequest' => function($params, $curlHttp) {
8+
//you need put you baidu api key here
9+
$apikey = '';
10+
echo $curlHttp->getMethod();
11+
echo $curlHttp->getUrl();
12+
$curlHttp->setHeader('apikey', $apikey);
13+
return $params;
14+
},
15+
'afterRequest' => function($response, $curlHttp) {
16+
// you may want process the request here, this is just a example
17+
$code = curl_getinfo($curlHttp->getCurl(), CURLINFO_HTTP_CODE);
18+
if($code == 200) {
19+
$data = json_decode($response, true);
20+
if(empty($data) || empty($data['code'])) {
21+
}
22+
return $response;
23+
}
24+
return $response;
1325
}
1426
]);
15-
$data = $curl->setPost()->httpExec("/", ['q' => 'test']);
27+
$data = $curl->setGet()
28+
->httpExec("/apistore/weatherservice/recentweathers", ['cityname' => '北京', 'cityid' => '101010100']);
29+
echo $data;

0 commit comments

Comments
 (0)