key = $str; return $this; } /** * 配置宝塔网址 * @param string $str * @return $this */ public function panel(string $str): self { $this->panel = $str; return $this; } /** * 配置网址 * @param string $str * @return $this */ public function url(string $str): self { $this->url = $str; return $this; } /** * 认证内容 * @param string $str * @return $this */ public function cookie(string $str): self { $this->cookie = $str; return $this; } /** * 超时,默认60s * @param int $int * @return $this */ public function timeout(int $int): self { $this->timeout = $int; return $this; } /** * 数据 * @param array $array * @return $this */ public function data(array $array): self { $this->data = $array; return $this; } /** * 返回数组数据 * @param bool $is * @return array|bool|mixed|string */ public function toArray(bool $is = true) { if (empty($this->cookie)) { throw new HttpException(404, '请检查cookie内容'); } if (!extension_loaded("curl")) { throw new HttpException(404, '请开启curl模块!'); } $this->http(); if (empty($is)) { return $this->output; } if (is_array($this->output)) { return $this->output; } return json_decode($this->output, true); } /** * 发起请求 * @return $this */ private function http(): self { $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); $this->output = $output; return $this; } /** * 构造带有签名的关联数组 * @return array */ private function getKeyData(): array { $time = time(); return array( 'request_token' => md5($time . '' . md5($this->key)), 'request_time' => $time ); } }