/** * 发布菜单 */ 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; }
/** * 获取响应 */ 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; }
/** * 获取 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; }
/** * 发送模板消息 */ 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']; }
/** * 获取 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; }
/** * 获取响应结果 */ 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; }
/** * 获取 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; }
/** * 批量获取用户信息 */ 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']); }
/** * 获取微信服务器 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']; }
/** * 获取短链接 */ 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']; }
/** * 获取响应结果 */ 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; }
/** * 查询订单 */ 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; }
/** * 通过 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()); }
/** * 检测用户 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'; }
/** * 移动用户分组 */ 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; }