/** * 上传媒体文件. * * @param string $path * * @return string * * @throws InvalidArgumentException */ public function upload($path) { if (!file_exists($path) || !is_readable($path)) { throw new InvalidArgumentException("文件不存在或不可读 '{$path}'"); } $options = ['files' => ['media' => $path]]; $contents = $this->http->post(self::API_UPLOAD, [], $options); return $contents['url']; }
/** * 上传媒体文件. * * @param string $type * @param string $path * @param array $params * * @return string * * @throws Exception */ protected function upload($type, $path, $params = []) { if (!file_exists($path) || !is_readable($path)) { throw new InvalidArgumentException("文件不存在或不可读 '{$path}'"); } if (!in_array($type, $this->allowTypes, true)) { throw new InvalidArgumentException("错误的媒体类型 '{$type}'"); } $queries = ['type' => $type]; $options = ['files' => ['media' => $path]]; $url = $this->getUrl($type, $queries); $response = $this->http->post($url, $params, $options); $this->forever = false; return $response['media_id']; }
/** * Set staff avatar. * * @param string $email * @param string $path * * @return bool */ public function avatar($email, $path) { $options = ['files' => ['media' => $path]]; $url = self::API_AVATAR_UPLOAD . "?kf_account={$email}"; return $this->http->post($url, [], $options); }