sendError('文件不存在', 404); } $access_token = $this->config->getAccessToken(); $client = new Client('api.weixin.qq.com', 443, true); $client->withHeader(['Content-Type' => 'multipart/form-data']); $client->upload($this->_url . '?access_token=' . $access_token, [ 'media' => new \CURLFile($path) ]); $client->close(); if (!in_array($client->getStatusCode(), [101, 200, 201])) { return new Result(code: 505, message: $client->getBody()); } $body = json_decode($client->getBody(), true); if (isset($body['errcode']) && $body['errcode'] != 0) { return new Result(code: $body['errcode'], message: $body['errmsg']); } return new Result(code: 0, data: $body); } /** * @param string $url * @param int $type * @return mixed * @throws */ public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result { if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) { throw new \Exception('暂不支持的文件类型'); } $requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken(); $client = new Client('api.weixin.qq.com', 443, true); $client->withHeader(['Content-Type' => 'application/json']); $client->post($requestUrl, ['media_url' => $url, 'media_type' => $type]); $client->close(); if (!in_array($client->getStatusCode(), [101, 200, 201])) { return new Result(code: 505, message: $client->getBody()); } $body = json_decode($client->getBody(), true); if (isset($body['errcode']) && $body['errcode'] != 0) { return new Result(code: $body['errcode'], message: $body['errmsg']); } else { return new Result(code: 0, data: $body); } } /** * @param $params * @return ContentAsyncCheck|null */ public function readByEvent($params): ?ContentAsyncCheck { return ContentAsyncCheck::instance($params); } /** * @param $content * @return Result */ public function text($content): Result { if (empty($content)) { return $this->sendError('文件不存在', 404); } $requestUrl = $this->_msgUrl . $this->config->getAccessToken(); $client = new Client('api.weixin.qq.com', 443, true); $client->withHeader(['Content-Type' => 'application/json']); $client->post($requestUrl, ['content' => $content]); $client->close(); if (!in_array($client->getStatusCode(), [101, 200, 201])) { return new Result(code: 505, message: $client->getBody()); } $body = json_decode($client->getBody(), true); if (isset($body['errcode']) && $body['errcode'] != 0) { return new Result(code: $body['errcode'], message: $body['errmsg']); } else { return new Result(code: 0, data: $body); } } }