/** * 刷新 */ public function refresh() { $request = Http::get(self::REFRESH_URL, array('query' => array('appid' => $this->wechat['appid'], 'grant_type' => 'refresh_token', 'refresh_token' => $this->options['refresh_token']))); $response = $request->json(); if (array_key_exists('access_token', $response) && array_key_exists('openid', $response)) { parent::__construct($response); return $this; } throw new \Exception($response['errcode'] . ': ' . $response['errmsg']); }
/** * 构造方法 */ public function __construct(AccessToken $accessToken) { if (!$accessToken->isValid()) { $accessToken->refresh(); } $request = Http::get(self::USERINFO_URL, array('query' => array('access_token' => $accessToken['access_token'], 'openid' => $accessToken['openid']))); $response = $request->json(); if (array_key_exists('errcode', $response) && array_key_exists('errmsg', $response)) { throw new \Exception($response['errcode'] . ': ' . $response['errmsg']); } $required = array('openid', 'nickname', 'sex', 'language', 'city', 'province', 'country', 'headimgurl'); $defined = array_merge($required, array('privilege', 'unionid')); $validator = new OptionValidator(); $validator->setDefined($defined)->setRequired($required); $validated = $validator->validate($response); parent::__construct($validated); }
/** * 构造方法 */ public function __construct(array $options) { $certNormalizer = function ($options, $value) { // authenticate_cert 为一个 包含 cert 和 key 的数组 if (!(array_key_exists('cert', $value) && array_key_exists('key', $value))) { throw new \InvalidArgumentException('Authenticate_cert cert/key is required'); } // authenticate_cert 中的 cert 和 key 必需为已存在的文件 if (!(file_exists($value['cert']) && file_exists($value['key']))) { throw new \InvalidArgumentException('Authenticate_cert cert/key is invalid file'); } }; $validator = new OptionValidator(); $validator->setDefined(array('appid', 'appsecret', 'mchid', 'mchkey', 'authenticate_cert'))->setRequired(array('appid', 'appsecret'))->setAllowedTypes('authenticate_cert', 'array')->setNormalizer('authenticate_cert', $certNormalizer); $options = $validator->validate($options); parent::__construct($options); }
/** * 构造方法 */ public function __construct(array $options = array()) { $options['MsgType'] = $this->getType(); parent::__construct($options); }