예제 #1
0
파일: Client.php 프로젝트: kevan/wechat-1
 /**
  * 授权回调(通过 Code 换取 Token)
  */
 public function getAccessToken($code)
 {
     $request = Http::get(self::ACCESS_TOKEN_URL, array('query' => array('appid' => $this->wechat['appid'], 'secret' => $this->wechat['appsecret'], 'code' => $code, 'grant_type' => 'authorization_code')));
     $response = $request->json();
     if (array_key_exists('access_token', $response) && array_key_exists('openid', $response)) {
         return $this->accessToken = new AccessToken($this->wechat, $response);
     }
     throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
 }
예제 #2
0
파일: Delete.php 프로젝트: kevan/wechat-1
 /**
  * 获取响应
  */
 public function doDelete()
 {
     $request = Http::get(self::DELETE_URL, array('query' => array('access_token' => $this->accessToken->getAccessToken())));
     $response = $request->json();
     if (array_key_exists('errcode', $response) && $response['errcode'] != 0) {
         throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
     }
     return true;
 }
예제 #3
0
 /**
  * 获取 AccessToken(从 API 获取)
  */
 protected function _getAccessToken()
 {
     $request = Http::get(self::ACCESS_TOKEN_URL, array('query' => array('grant_type' => 'client_credential', 'appid' => $this->wechat['appid'], 'secret' => $this->wechat['appsecret'])));
     $response = $request->json();
     if (array_key_exists('access_token', $response) && array_key_exists('expires_in', $response)) {
         return $response;
     }
     throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
 }
예제 #4
0
파일: Ticket.php 프로젝트: kevan/wechat-1
 /**
  * 获取 Ticket(从 API 获取)
  */
 protected function _getTicket($type)
 {
     $request = Http::get(self::TICKET_URL, array('query' => array('access_token' => $this->accessToken->getAccessToken(), 'type' => $type)));
     $response = $request->json();
     if (array_key_exists('ticket', $response) && array_key_exists('expires_in', $response)) {
         return $response;
     }
     throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
 }
예제 #5
0
파일: ServerIp.php 프로젝트: kevan/wechat-1
 /**
  * 获取服务器 IP 列表
  */
 public function getServerIp()
 {
     $request = Http::get(self::SERVERIP_URL, array('query' => array('access_token' => $this->accessToken->getAccessToken())));
     $response = $request->json();
     if (array_key_exists('ip_list', $response)) {
         return $response['ip_list'];
     }
     throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
 }
예제 #6
0
파일: User.php 프로젝트: Oppaa/wechat
 /**
  * 构造方法
  */
 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']);
     }
     parent::__construct($response);
 }
예제 #7
0
 /**
  * 获取下单结果
  */
 public function getResponse()
 {
     ksort($this->options);
     $signature = http_build_query($this->options);
     $signature = urldecode($signature);
     $signature = strtoupper(md5($signature . '&key=' . $this->wechat['mchkey']));
     $this->options['sign'] = $signature;
     $request = Http::post(self::UNIFIEDORDER_URL, array('body' => Serialize::encode($this->options, 'xml')));
     $response = (array) $request->xml(array('libxml_options' => LIBXML_NOCDATA));
     if (array_key_exists('result_code', $response) && $response['result_code'] === 'FAIL') {
         throw new \Exception($response['err_code'] . ': ' . $response['err_code_des']);
     }
     if (array_key_exists('return_code', $response) && $response['return_code'] === 'FAIL') {
         throw new \Exception($response['return_code'] . ': ' . $response['return_msg']);
     }
     return $response;
 }
예제 #8
0
파일: User.php 프로젝트: kevan/wechat-1
 /**
  * 构造方法
  */
 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);
 }
예제 #9
0
 /**
  * 检测是否有效
  */
 public function isValid()
 {
     $request = Http::get(self::CHECK_URL, array('query' => array('access_token' => $this->options['access_token'], 'openid' => $this->options['openid'])));
     $response = $request->json();
     return $response['errmsg'] === 'ok';
 }
예제 #10
0
파일: OAuth.php 프로젝트: kendoctor/wechat
 /**
  * 获取当前(已授权)用户信息
  */
 public function getUser($token)
 {
     $params = array('access_token' => $token->access_token, 'openid' => $token->openid);
     $response = Http::get(static::USERINFO_URL, $params);
     if (isset($response->openid) && isset($response->nickname) && isset($response->headimgurl)) {
         return $response;
     }
     throw new OAuthException($response->errcode . ': ' . $response->errmsg);
 }
예제 #11
0
파일: Wechat.php 프로젝트: kendoctor/wechat
 /**
  * 获取微信服务器 IP
  */
 public function getServerIp()
 {
     try {
         $accessToken = $this->getAccessToken();
     } catch (AccessTokenException $e) {
         throw new WechatException($e->getMessage());
     }
     $params = array('access_token' => $accessToken);
     $response = Http::get(static::SERVERIP_URL, $params);
     if (isset($response->ip_list)) {
         return $response->ip_list;
     }
     throw new WechatException($response->errcode . ': ' . $response->errmsg);
 }
예제 #12
0
파일: Template.php 프로젝트: kevan/wechat-1
 /**
  * 发送模板消息
  */
 public function send()
 {
     if (empty($this->options) || $this->options->isEmpty()) {
         throw new \Exception('Options is requried');
     }
     $body = array('touser' => $this->touser, 'template_id' => $this->templateId, 'url' => $this->url, 'data' => $this->options->toArray());
     $request = Http::post(self::SEND_URL, array('body' => Serialize::encode($body, 'json'), 'query' => array('access_token' => $this->accessToken->getAccessToken())));
     $response = $request->json();
     if (array_key_exists('errcode', $response) && $response['errcode'] != 0) {
         throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
     }
     return $response['msgid'];
 }
예제 #13
0
 /**
  * 获取统一下单结果
  */
 public function getResponse()
 {
     $signGenerator = new SignGenerator($this->bag);
     $signGenerator->onSortAfter(function ($bag) {
         $bag->set('key', $this->key);
     });
     // 生成签名
     $sign = $signGenerator->getResult();
     // 生成签名后移除 Key
     $this->bag->remove('key');
     // 调置签名
     $this->bag->set('sign', $sign);
     $body = Util::array2XML($this->bag->all());
     $response = Http::post(static::UNIFIEDORDER_URL, $body, false);
     $response = Util::XML2Array($response);
     if (isset($response['result_code']) && $response['result_code'] === 'FAIL') {
         throw new PaymentException($response['err_code'] . ': ' . $response['err_code_des']);
     }
     if (isset($response['return_code']) && $response['return_code'] === 'FAIL') {
         throw new PaymentException($response['return_code'] . ': ' . $response['return_msg']);
     }
     return $response;
 }