GC0202 /
ThinkLibrary
| 1 | <?php |
||||
| 2 | |||||
| 3 | // +---------------------------------------------------------------------- |
||||
| 4 | // | ThinkLibrary 6.0 for ThinkPhP 6.0 |
||||
| 5 | // +---------------------------------------------------------------------- |
||||
| 6 | // | 版权所有 2017~2020 [ https://www.dtapp.net ] |
||||
| 7 | // +---------------------------------------------------------------------- |
||||
| 8 | // | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
||||
| 9 | // +---------------------------------------------------------------------- |
||||
| 10 | // | 开源协议 ( https://mit-license.org ) |
||||
| 11 | // +---------------------------------------------------------------------- |
||||
| 12 | // | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
||||
| 13 | // | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
||||
| 14 | // | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
||||
| 15 | // +---------------------------------------------------------------------- |
||||
| 16 | |||||
| 17 | namespace DtApp\ThinkLibrary\service\curl; |
||||
| 18 | |||||
| 19 | use DtApp\ThinkLibrary\Service; |
||||
| 20 | use think\exception\HttpException; |
||||
| 21 | |||||
| 22 | /** |
||||
| 23 | * 宝塔网络请求接口 |
||||
| 24 | * https://www.bt.cn/ |
||||
| 25 | * Class BtService |
||||
| 26 | * @package DtApp\ThinkLibrary\service\curl |
||||
| 27 | */ |
||||
| 28 | class BtService extends Service |
||||
| 29 | { |
||||
| 30 | /** |
||||
| 31 | * @var |
||||
| 32 | */ |
||||
| 33 | private $url, $key, $panel, $output, $cookie; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * @var array |
||||
| 37 | */ |
||||
| 38 | private $data = []; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * @var int |
||||
| 42 | */ |
||||
| 43 | private $timeout = 60; |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * 配置宝塔密钥 |
||||
| 47 | * @param string $str |
||||
| 48 | * @return $this |
||||
| 49 | */ |
||||
| 50 | public function key(string $str): self |
||||
| 51 | { |
||||
| 52 | $this->key = $str; |
||||
| 53 | return $this; |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * 配置宝塔网址 |
||||
| 58 | * @param string $str |
||||
| 59 | * @return $this |
||||
| 60 | */ |
||||
| 61 | public function panel(string $str): self |
||||
| 62 | { |
||||
| 63 | $this->panel = $str; |
||||
| 64 | return $this; |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * 配置网址 |
||||
| 69 | * @param string $str |
||||
| 70 | * @return $this |
||||
| 71 | */ |
||||
| 72 | public function url(string $str): self |
||||
| 73 | { |
||||
| 74 | $this->url = $str; |
||||
| 75 | return $this; |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * 认证内容 |
||||
| 80 | * @param string $str |
||||
| 81 | * @return $this |
||||
| 82 | */ |
||||
| 83 | public function cookie(string $str): self |
||||
| 84 | { |
||||
| 85 | $this->cookie = $str; |
||||
| 86 | return $this; |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | /** |
||||
| 90 | * 超时,默认60s |
||||
| 91 | * @param int $int |
||||
| 92 | * @return $this |
||||
| 93 | */ |
||||
| 94 | public function timeout(int $int): self |
||||
| 95 | { |
||||
| 96 | $this->timeout = $int; |
||||
| 97 | return $this; |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * 数据 |
||||
| 102 | * @param array $array |
||||
| 103 | * @return $this |
||||
| 104 | */ |
||||
| 105 | public function data(array $array): self |
||||
| 106 | { |
||||
| 107 | $this->data = $array; |
||||
| 108 | return $this; |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * 返回数组数据 |
||||
| 113 | * @param bool $is |
||||
| 114 | * @return array|bool|mixed|string |
||||
| 115 | */ |
||||
| 116 | public function toArray(bool $is = true) |
||||
| 117 | { |
||||
| 118 | if (empty($this->cookie)) { |
||||
| 119 | throw new HttpException(404, '请检查cookie内容'); |
||||
| 120 | } |
||||
| 121 | if (!extension_loaded("curl")) { |
||||
| 122 | throw new HttpException(404, '请开启curl模块!'); |
||||
| 123 | } |
||||
| 124 | $this->http(); |
||||
| 125 | if (empty($is)) { |
||||
| 126 | return $this->output; |
||||
| 127 | } |
||||
| 128 | if (is_array($this->output)) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 129 | return $this->output; |
||||
| 130 | } |
||||
| 131 | return json_decode($this->output, true); |
||||
|
0 ignored issues
–
show
It seems like
$this->output can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 132 | } |
||||
| 133 | |||||
| 134 | /** |
||||
| 135 | * 发起请求 |
||||
| 136 | * @return $this |
||||
| 137 | */ |
||||
| 138 | private function http(): self |
||||
| 139 | { |
||||
| 140 | $ch = curl_init(); |
||||
| 141 | curl_setopt($ch, CURLOPT_URL, $this->panel . $this->url); |
||||
| 142 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
||||
| 143 | curl_setopt($ch, CURLOPT_POST, 1); |
||||
| 144 | curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge($this->getKeyData(), $this->data)); |
||||
| 145 | curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); |
||||
| 146 | curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); |
||||
| 147 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||||
| 148 | curl_setopt($ch, CURLOPT_HEADER, 0); |
||||
| 149 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
||||
| 150 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||
| 151 | $output = curl_exec($ch); |
||||
| 152 | curl_close($ch); |
||||
| 153 | $this->output = $output; |
||||
| 154 | return $this; |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | /** |
||||
| 158 | * 构造带有签名的关联数组 |
||||
| 159 | * @return array |
||||
| 160 | */ |
||||
| 161 | private function getKeyData(): array |
||||
| 162 | { |
||||
| 163 | $time = time(); |
||||
| 164 | return array( |
||||
| 165 | 'request_token' => md5($time . '' . md5($this->key)), |
||||
| 166 | 'request_time' => $time |
||||
| 167 | ); |
||||
| 168 | } |
||||
| 169 | } |
||||
| 170 |