Skip to content

Commit a0de484

Browse files
author
lishipeng
committed
添加curl命令
1 parent d18f03f commit a0de484

File tree

4 files changed

+210
-1
lines changed

4 files changed

+210
-1
lines changed

BaseCurlHttp.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public function refreshCurl()
269269
$this->_curl = null;
270270
}
271271

272-
public static function requestByUrl($url, $params = [], $method = self::METHOD_GET)
272+
public static function getObjectByUrl($url, &$action=null, $method = self::METHOD_GET)
273273
{
274274
$data = parse_url($url);
275275
$config = [];
@@ -288,6 +288,13 @@ public static function requestByUrl($url, $params = [], $method = self::METHOD_G
288288
}
289289
$config['class'] = get_called_class();
290290
$obj = Yii::createObject($config);
291+
return $obj;
292+
}
293+
294+
public static function requestByUrl($url, $params = [], $method = self::METHOD_GET)
295+
{
296+
$action = null;
297+
$obj = self::getObjectByUrl($url, $action, $method);
291298
return $obj->httpExec($action, $params);
292299
}
293300
}

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,32 @@ Usage
117117
- 如果需要传文件,我们需要formData方式时,可以加上setFormData()来实现。
118118
- 正常如果beforeRequest和AfterRequest较长的话,不建议写配置中。建议通过类的继承来实现。
119119

120+
4、console模式
121+
正常情况下,我们经常需要在命令行发出一些命令,来跟baidu通信,调试api接口。因此,我们可以使用代码提供的工具来调试:
122+
123+
首先,在`console/controllers/main-local.php`中加入如下的配置:
124+
125+
```php
126+
return [
127+
// 其它配置
128+
'modules' => [
129+
// ...
130+
'curl' => ['class' => 'lspbupt\curl\module\Module'],
131+
// ...
132+
],
133+
// 其它配置
134+
];
135+
```
136+
137+
其次,我们就可以在命令行使用它进行调试了:
138+
139+
```bash
140+
./yii curl baiduApi "/apistore/iplookupservice/iplookup?ip=1.1.1.1"
141+
#更多帮助
142+
./yii curl -h
143+
```
144+
145+
120146
广告
121147
--------------
122148

module/Module.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace lspbupt\curl\module;
3+
4+
use Yii;
5+
use yii\base\BootstrapInterface;
6+
use yii\helpers\Json;
7+
use yii\web\ForbiddenHttpException;
8+
9+
/**
10+
* To use Curl, include it as a module in the application configuration like the following:
11+
*
12+
* ~~~
13+
* return [
14+
* 'modules' => [
15+
* 'curl' => ['class' => 'yii\curl\Module'],
16+
* ],
17+
* ]
18+
* ~~~
19+
*
20+
* @author Qiang Xue <qiang.xue@gmail.com>
21+
* @since 2.0
22+
*/
23+
class Module extends \yii\base\Module implements BootstrapInterface
24+
{
25+
public $controllerNamespace = 'lspbupt\curl\module\controllers';
26+
27+
28+
public function bootstrap($app)
29+
{
30+
}
31+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
namespace lspbupt\curl\module\controllers;
3+
4+
use Yii;
5+
use lspbupt\curl\CurlHttp;
6+
use yii\di\Instance;
7+
use yii\validators\UrlValidator;
8+
9+
class DefaultController extends \yii\console\Controller
10+
{
11+
/**
12+
* @var string 对应curl的--data-raw, -d选项。后面可以跟"a=b&c=d"也可以跟json数据
13+
*/
14+
public $dataRaw = "";
15+
/**
16+
* @var bool 对应curl -v, --verbose选项。如果设置,会打印出整个请求的过程
17+
*/
18+
public $verbose = false;
19+
/**
20+
* @var string 对应curl的-H, --header选项。注意:不能多个-H使用,多个header请用|分隔,如:"Content-type: xxx|User-Agent: xxx"
21+
*/
22+
public $header = "";
23+
/**
24+
* @var string 对应curl的-x, --request选项。本软件只支付get, post, postjson, formdata。如果不设置,系统会自行判断
25+
*/
26+
public $request = "";
27+
28+
public static $allowMethods = ["get", "post", "postjson", "formdata"];
29+
30+
public $defaultScheme = "http";
31+
32+
/**
33+
* 通过系统的实例来发起http请求
34+
*/
35+
public function actionIndex($instance, $action = "")
36+
{
37+
$params = [];
38+
//规范化method
39+
$method = "GET";
40+
$isFormData = false;
41+
if($this->dataRaw !== "") {
42+
$method = "POST";
43+
//判断dataRaw是否为json
44+
$ret = json_decode($this->dataRaw);
45+
if(!is_null($ret)) {
46+
$method = "POSTJSON";
47+
$params = $ret;
48+
} else {
49+
parse_str($this->dataRaw, $params);
50+
if(is_null($params)) {
51+
echo "-d 参数错误,请传输有意义的data数据";
52+
return 1;
53+
}
54+
foreach($params as $key => $value) {
55+
if(is_string($value) && substr($value, 0, 1) === "@") {
56+
$method = "POST";
57+
$isFormData = true;
58+
$file = substr($value, 1);
59+
$params[$key] = new \CURLFile(realpath($file));
60+
}
61+
}
62+
}
63+
}
64+
65+
//规范化method
66+
empty($this->request) && $this->request = $method;
67+
$this->request = strtolower($this->request);
68+
if(!in_array($this->request, self::$allowMethods)) {
69+
$this->request = "get";
70+
}
71+
//规范化headers
72+
$this->header = $this->parseHeader($this->header);
73+
74+
75+
$obj = $this->getInstance($instance, $action);
76+
call_user_func([$obj, "set".$this->request]);
77+
$ret = $obj->setDebug((bool)$this->verbose)
78+
->setHeaders($this->header)
79+
->setFormData($isFormData)
80+
->httpExec($action, $params);
81+
if(!$this->verbose) {
82+
if(is_string($ret)) {
83+
echo $ret;
84+
}else {
85+
var_dump($ret);
86+
}
87+
}
88+
return 0;
89+
}
90+
91+
public function options($actionID)
92+
{
93+
return array_merge(parent::options($actionID), [
94+
'dataRaw',
95+
'verbose',
96+
'header',
97+
'request'
98+
]);
99+
}
100+
101+
public function optionAliases()
102+
{
103+
return array_merge(parent::optionAliases(), [
104+
'd' => 'data-raw',
105+
'v' => 'verbose',
106+
'H' => 'header',
107+
'X' => 'request'
108+
]);
109+
}
110+
111+
private function parseHeader($headers)
112+
{
113+
$headers = explode("|", $headers);
114+
$arr = [];
115+
foreach($headers as $str) {
116+
$index = strpos($str, ":");
117+
if($index !== false) {
118+
$headKey = trim(substr($str, 0, $index));
119+
$headValue = trim(substr($str, $index+1));
120+
$arr[$headKey] = $headValue;
121+
}
122+
}
123+
124+
return $arr;
125+
}
126+
127+
private function getInstance($key, &$action)
128+
{
129+
$key = trim($key);
130+
$obj = null;
131+
//先判断是否为Url
132+
$validator = new UrlValidator([
133+
'defaultScheme' => $this->defaultScheme,
134+
]);
135+
if($validator->validate($key)) {
136+
if(strpos($key, '://') === false) {
137+
$key = $this->defaultScheme . "://". $key;
138+
}
139+
$obj = CurlHttp::getObjectByUrl($key, $action);
140+
}else {
141+
$obj = Instance::ensure($key);
142+
}
143+
return $obj;
144+
}
145+
}

0 commit comments

Comments
 (0)