Beispiel #1
0
 /**
  * 发布菜单
  */
 public function doCreate()
 {
     $response = Http::request('POST', static::CREATE_URL)->withAccessToken($this->accessToken)->withBody($this->getRequestBody())->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return true;
 }
Beispiel #2
0
 /**
  * 获取响应
  */
 public function doDelete()
 {
     $response = Http::request('GET', static::DELETE_URL)->withAccessToken($this->accessToken)->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return true;
 }
Beispiel #3
0
 /**
  * 获取 Qrcode 票据(不缓存,返回原始数据)
  */
 public function getTicketResponse()
 {
     $response = Http::request('POST', static::TICKET_URL)->withAccessToken($this->accessToken)->withBody($this->getRequestBody())->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return $response;
 }
Beispiel #4
0
 /**
  * 发送模板消息
  */
 public function send(TemplateInterface $template)
 {
     $response = Http::request('POST', static::SENDER)->withAccessToken($this->accessToken)->withBody($template->getRequestBody())->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return $response['msgid'];
 }
Beispiel #5
0
 /**
  * 获取 Jsapi 票据(不缓存,返回原始数据)
  */
 public function getTicketResponse()
 {
     $response = Http::request('GET', static::JSAPI_TICKET)->withAccessToken($this->accessToken)->withQuery(array('type' => 'jsapi'))->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return $response;
 }
Beispiel #6
0
 /**
  * 获取响应结果
  */
 public function doQuery()
 {
     $response = Http::request('GET', static::QUERY_URL)->withAccessToken($this->accessToken)->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return $response;
 }
Beispiel #7
0
 /**
  * 获取 AccessToken(不缓存,返回原始数据)
  */
 public function getTokenResponse()
 {
     $query = array('grant_type' => 'client_credential', 'appid' => $this['appid'], 'secret' => $this['appsecret']);
     $response = Http::request('GET', static::ACCESS_TOKEN)->withQuery($query)->send();
     if ($response->containsKey('errcode')) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return $response;
 }
Beispiel #8
0
 /**
  * 批量获取用户信息
  */
 public function getBetch(array $openid, $lang = 'zh_CN')
 {
     $body = array();
     foreach ($openid as $key => $value) {
         $body['user_list'][$key]['openid'] = $value;
         $body['user_list'][$key]['lang'] = $lang;
     }
     $response = Http::request('POST', static::BETCH)->withAccessToken($this->accessToken)->withBody($body)->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return new ArrayCollection($response['user_info_list']);
 }
Beispiel #9
0
 /**
  * 获取微信服务器 IP(默认缓存 1 天)
  */
 public function getIps($cacheLifeTime = 86400)
 {
     $cacheId = $this->getCacheId();
     if ($this->cache && ($data = $this->cache->fetch($cacheId))) {
         return $data['ip_list'];
     }
     $response = Http::request('GET', static::SERVER_IP)->withAccessToken($this->accessToken)->send();
     if ($response->containsKey('errcode')) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     if ($this->cache) {
         $this->cache->save($cacheId, $response, $cacheLifeTime);
     }
     return $response['ip_list'];
 }
Beispiel #10
0
 /**
  * 获取短链接
  */
 public function toShort($longUrl, $cacheLifeTime = 86400)
 {
     $cacheId = md5($longUrl);
     if ($this->cache && ($data = $this->cache->fetch($cacheId))) {
         return $data;
     }
     $body = array('action' => 'long2short', 'long_url' => $longUrl);
     $response = Http::request('POST', static::SHORT_URL)->withAccessToken($this->accessToken)->withBody($body)->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     if ($this->cache) {
         $this->cache->save($cacheId, $response['short_url'], $cacheLifeTime);
     }
     return $response['short_url'];
 }
Beispiel #11
0
 /**
  * 获取响应结果
  */
 public function getResponse()
 {
     $options = $this->resolveOptions();
     // 按 ASCII 码排序
     ksort($options);
     $signature = urldecode(http_build_query($options));
     $signature = strtoupper(md5($signature . '&key=' . $this->key));
     $options['sign'] = $signature;
     $response = Http::request('POST', static::COUPON_CASH)->withSSLCert($this->sslCert, $this->sslKey)->withXmlBody($options)->send();
     if ($response['return_code'] === 'FAIL') {
         throw new \Exception($response['return_msg']);
     }
     if ($response['result_code'] === 'FAIL') {
         throw new \Exception($response['err_code_des']);
     }
     return $response;
 }
Beispiel #12
0
 /**
  * 查询订单
  */
 public function doQuery(array $by)
 {
     $options = array_merge($this->toArray(), $by);
     $options['nonce_str'] = Util::getRandomString();
     // 按 ASCII 码排序
     ksort($options);
     $signature = urldecode(http_build_query($options));
     $signature = strtoupper(md5($signature . '&key=' . $this->key));
     $options['sign'] = $signature;
     $response = Http::request('POST', static::QUERY)->withXmlBody($options)->send();
     if ($response['result_code'] === 'FAIL') {
         throw new \Exception($response['err_code_des']);
     }
     if ($response['return_code'] === 'FAIL') {
         throw new \Exception($response['return_msg']);
     }
     return $response;
 }
Beispiel #13
0
 /**
  * 通过 code 换取 AccessToken
  */
 public function getAccessToken($code, $state = null)
 {
     if (null === $state && !isset($_GET['state'])) {
         throw new \Exception('Invalid Request');
     }
     // http://www.twobotechnologies.com/blog/2014/02/importance-of-state-in-oauth2.html
     $state = $state ?: $_GET['state'];
     if (!$this->stateManager->isValid($state)) {
         throw new \Exception(sprintf('Invalid Authentication State "%s"', $state));
     }
     $query = array('appid' => $this->appid, 'secret' => $this->appsecret, 'code' => $code, 'grant_type' => 'authorization_code');
     $response = Http::request('GET', static::ACCESS_TOKEN)->withQuery($query)->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return new AccessToken($this->appid, $response->toArray());
 }
Beispiel #14
0
 /**
  * 检测用户 access_token 是否有效
  */
 public function isValid()
 {
     $query = array('access_token' => $this['access_token'], 'openid' => $this['openid']);
     $response = Http::request('GET', static::IS_VALID)->withQuery($query)->send();
     return $response['errmsg'] === 'ok';
 }
Beispiel #15
0
 /**
  * 移动用户分组
  */
 public function updateUserGroup($openid, $newId)
 {
     $key = is_array($openid) ? 'openid_list' : 'openid';
     $api = is_array($openid) ? static::BETCH_UPDATE_USER_GROUP : static::UPDATE_USER_GROUP;
     $body = array($key => $openid, 'to_groupid' => $newId);
     $response = Http::request('POST', $api)->withAccessToken($this->accessToken)->withBody($body)->send();
     if ($response['errcode'] != 0) {
         throw new \Exception($response['errmsg'], $response['errcode']);
     }
     return true;
 }