|
| 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