diff --git a/composer.json b/composer.json index 770723b..f94d3a1 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "ext-openssl": "*", "ext-bcmath": "*", "ext-iconv": "*", + "ext-curl": "*", "topthink/framework": "^6.0.0", "topthink/think-orm": "^2.0", "aliyuncs/oss-sdk-php": "^2.3", diff --git a/src/exception/CurlException.php b/src/exception/CurlException.php new file mode 100644 index 0000000..76cf5c8 --- /dev/null +++ b/src/exception/CurlException.php @@ -0,0 +1,32 @@ +getMessage(); + } +} diff --git a/src/service/curl/BtService.php b/src/service/curl/BtService.php new file mode 100644 index 0000000..3725a59 --- /dev/null +++ b/src/service/curl/BtService.php @@ -0,0 +1,141 @@ +key = $str; + return $this; + } + + /** + * 配置宝塔网址 + * @param string $str + * @return $this + */ + public function panel(string $str) + { + $this->panel = $str; + return $this; + } + + /** + * 配置网址 + * @param string $str + * @return $this + */ + public function url(string $str) + { + $this->url = $str; + return $this; + } + + /** + * 认证内容 + * @param string $str + * @return $this + */ + public function cookie(string $str) + { + $this->cookie = $str; + return $this; + } + + /** + * 超时,默认60s + * @param int $int + * @return $this + */ + public function timeout(int $int) + { + $this->timeout = $int; + return $this; + } + + /** + * 超时,数据 + * @param array $array + * @return $this + */ + public function data(array $array) + { + $this->data = $array; + return $this; + } + + /** + * 返回数组数据 + * @param bool $is + * @return array|bool|mixed|string + * @throws CurlException + */ + public function toArray(bool $is = true) + { + if (empty($this->cookie)) throw new CurlException('请检查cookie内容'); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->panel . $this->url); + curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge($this->getKeyData(), $this->data)); + curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); + curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + $output = curl_exec($ch); + curl_close($ch); + if (empty($is)) return $output; + try { + if (is_array($output)) return $output; + return json_decode($output, true); + } catch (Exception $e) { + return false; + } + } + + /** + * 构造带有签名的关联数组 + * @return array + */ + private function getKeyData() + { + $time = time(); + return array( + 'request_token' => md5($time . '' . md5($this->key)), + 'request_time' => $time + ); + } +}