/** * 构造方法 */ function __construct($accessToken, $openid) { $params = array('access_token' => $accessToken, 'openid' => $openid); $curl = new Curl(); $response = $curl->get(static::USERINFO_URL, $params); if (empty($response)) { throw new OAuthException('Get Userinfo Failure: ' . $curl->error()); } $object = Json::decode($response); if (!(isset($object->openid) || isset($object->nickname) || isset($object->headimgurl))) { throw new OAuthException($object->errcode . ': ' . $object->errmsg); } $this->params = $object; }
/** * 授权回调(通过 Code 换取 AccessToken) * */ public function getAccessToken($code) { $params = array('appid' => $this->appid, 'secret' => $this->appsecret, 'code' => $code, 'grant_type' => 'authorization_code'); $curl = new Curl(); $response = $curl->get(static::ACCESS_TOKEN_URL, $params); if (empty($response)) { throw new OAuthException('Get AccessToken Failure: ' . $curl->error()); } $object = Json::decode($response); if (isset($object->access_token) && isset($object->expires_in) && isset($object->openid)) { return $object; } throw new OAuthException($object->errcode . ': ' . $object->errmsg); }