/** * 设置客服帐号头像 * @param string $account 客服帐号(可忽略后缀) * @param string $img 头像图片文件 * @return boolean true表示成功 * @throws ApiException */ public function uploadHeadimg($account, $img) { $url = $this->url . '/uploadheadimg?access_token=' . $this->accessToken . '&kf_account=' . $this->addPostfix($account); $httpClient = new HttpClient($url); $httpClient->upload(array('media' => $img)); return $this->checkErrcode($httpClient); }
/** * 上传一个素材 * @param string $mediaFile 完整(绝对路径)文件路径 * @param string $type 可选,上传媒体文件的类型(image、voice、video、thumb),不填则根据后缀名判断 * @return string mediaId媒体id(请求失败返回false) * @throws ApiException */ public function upload($mediaFile, $type = null) { if (!file_exists($mediaFile)) { throw ApiException::throws(ApiException::FILE_NOT_EXISTS_ERROR_CODE, "file: {$mediaFile}"); return false; } if ($type == null) { $type = $this->parseMediaType($mediaFile); } $url = $this->url . '/upload?access_token=' . $this->accessToken . '&type=' . $type; $httpClient = new HttpClient($url); $httpClient->upload(array('media' => $mediaFile)); $result = $httpClient->jsonToArray(); if (isset($result['media_id'])) { return $result['media_id']; } else { throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } //never return false; }