/** * 通过调用信任登陆accesstoken接口生成access token * * @param string $code * @return string */ public function generateAccessToken($code) { $args = ['client_id' => $this->getAppKey(), 'client_secret' => $this->getAppSecret(), 'grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => $this->getCallbackUrl()]; $msg = client::post($this->getUrl('token'), ['body' => $args])->json(); if (isset($msg['error'])) { throw new \LogicException("error :" . $msg['error_code'] . "msg :" . $msg['error']); } return $msg['access_token']; }
/** * 生成信任登陆open id * * @return string */ public function generateOpenId() { $args = ['access_token' => $this->getAccessToken()]; $msg = client::post($this->getUrl('openid'), ['body' => $args])->json(); if (isset($msg['error'])) { throw new \LogicException("error :" . $msg['error_code'] . "msg :" . $msg['error']); } return $msg['uid']; }
/** * 通过调用信任登陆accesstoken接口生成access token * * @param string $code * @return string */ public function generateAccessToken($code) { $args = ['client_id' => $this->getAppKey(), 'client_secret' => $this->getAppSecret(), 'grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => $this->getCallbackUrl()]; try { $msg = client::post($this->getUrl('token'), ['body' => $args])->json(); } catch (ClientException $e) { $msg = $e->getResponse()->json(); throw new \LogicException("error :" . $msg['error'] . "msg :" . $msg['error_description']); } $this->taobaoUserInfo = ['taobao_user_id' => $msg['taobao_user_id'], 'taobao_user_nick' => $msg['taobao_user_nick']]; return $msg['access_token']; }
/** * 上传文件到七牛,内部使用 * * @param $upToken 上传凭证 * @param $key 上传文件名 * @param $filePath 上传文件的路径 * @param $params 自定义变量,规格参考 * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar * @param $mime 上传数据的mimeType * @param $checkCrc 是否校验crc32 * * @return array 包含已上传文件的信息,类似: * [ * "hash" => "<Hash string>", * "key" => "<Key string>" * ] */ public static function putFile($upToken, $key, $filePath, $config, $params, $mime, $checkCrc) { $fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime)); if ($key !== null) { $fields['key'] = $key; } if ($checkCrc) { $fields['crc32'] = Qiniu\crc32_file($filePath); } if ($params) { foreach ($params as $k => $v) { $fields[$k] = $v; } } $fields['key'] = $key; $headers = array('Content-Type' => 'multipart/form-data'); $response = client::post($config->getUpHost(), $fields, $headers); if (!$response->ok()) { return array(null, new Error($config->getUpHost(), $response)); } return array($response->json(), null); }
public static function putFile($upToken, $key, $filePath, $params, $mime, $checkCrc) { $fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime)); if ($key === null) { $fname = 'filename'; } else { $fname = $key; $fields['key'] = $key; } if ($checkCrc) { $fields['crc32'] = (new Functions())->crc32_file($filePath); } if ($params) { foreach ($params as $k => $v) { $fields[$k] = $v; } } $headers = array('Content-Type' => 'multipart/form-data'); $response = client::post(Config::$defaultHost, $fields, $headers); if (!$response->ok()) { return array(null, new Error(Config::$defaultHost, $response)); } return array($response->json(), null); }