Пример #1
0
 /**
  * 授权回调(通过 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);
 }
Пример #2
0
 /**
  * 构造方法
  */
 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;
 }
Пример #3
0
 /**
  * 获取用户信息
  */
 public function getUser($openid, $accessToken)
 {
     $params = array('access_token' => $accessToken, 'openid' => $openid);
     $response = $this->curl->get(static::USERINFO, $params);
     if (empty($response)) {
         throw new OAuthException('Get Userinfo Failure: ' . $this->curl->error());
     }
     $object = Json::decode($response);
     if (isset($object->openid) && isset($object->nickname) && isset($object->headimgurl)) {
         return $object;
     }
     throw new OAuthException("{$object->errcode}: {$object->errmsg}");
 }
Пример #4
0
 /**
  * 获取 Ticket(内部方法)
  */
 private function _getTicketResponse($type)
 {
     if (null === $this->accessToken) {
         $accessToken = $this->getAccessToken();
         $this->setAccessToken($accessToken);
     }
     $params = array('access_token' => $this->accessToken, 'type' => $type);
     $response = $this->curl->get(static::TICKET_URL, $params);
     if (empty($response)) {
         throw new TicketException(sprintf('Get Ticket Failure: %s', $this->curl->error()));
     }
     $object = Json::decode($response);
     if (isset($object->ticket) && isset($object->expires_in)) {
         return $object;
     }
     throw new TicketException("{$object->errcode}: {$object->errmsg}");
 }