Esempio n. 1
0
 /**
  * 获取IP列表
  * @return array 对应ip地址数组(请求失败返回false)
  * @throws ApiException
  */
 public function get()
 {
     $httpClient = new HttpClient($this->url);
     $httpClient->get();
     $data = $httpClient->jsonToArray();
     if (isset($data['ip_list'])) {
         return $data['ip_list'];
     } else {
         throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse());
     }
     //never
     return false;
 }
Esempio n. 2
0
 private function reloadTicket()
 {
     $fp = fopen($this->file, "w");
     if (flock($fp, LOCK_EX)) {
         Log::i('Lock', 'JsapiTicket');
         try {
             $url = $this->url . "?access_token={$this->accessToken}&type=wx_card";
             $httpClient = new HttpClient($url);
             $httpClient->get();
             $stream = $httpClient->jsonToArray();
             if (isset($stream['ticket'])) {
                 //提取参数
                 $jsapi_ticket = $stream['ticket'];
                 $expires_in = $stream['expires_in'];
                 $expires_time = intval(time()) + intval($expires_in) - 60;
                 //60s超时缓冲
                 $file_stream = json_encode(array('expires_time' => $expires_time, 'jsapi_ticket' => $jsapi_ticket));
                 fwrite($fp, "<?php exit; ?>\n");
                 fwrite($fp, $file_stream);
                 Log::i('Unlock', 'JsapiTicket');
                 flock($fp, LOCK_UN);
                 fclose($fp);
                 @chmod($this->file, DEFAULT_PERMISSION);
                 return $jsapi_ticket;
             } else {
                 throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse());
             }
         } catch (ApiException $e) {
             Log::i('Unlock', 'JsapiTicket');
             flock($fp, LOCK_UN);
             fclose($fp);
             throw $e;
             return false;
         }
     } else {
         fclose($fp);
         throw ApiException::throws(ApiException::FILE_LOCK_ERROR_CODE);
         return false;
     }
 }
Esempio n. 3
0
 /**
  * 下载一个素材
  * @param string $mediaId 媒体id,通过上传获得
  * @param string $toFile 可选,下载到文件的绝对路径,不填则默认路径为RES_ROOT/$mediaId
  * @return integer 下载到的文件大小(失败时返回false)
  * @throws ApiException
  */
 public function download($mediaId, $toFile = null)
 {
     //使用http协议
     // $this->url = str_replace('https', 'http', $this->url);
     $url = $this->url . '/get?access_token=' . $this->accessToken . '&media_id=' . $mediaId;
     $httpClient = new HttpClient($url);
     $httpClient->get(30);
     if ($httpClient->getStatus() != 200 || $httpClient->getResponse() == '') {
         throw ApiException::throws(ApiException::HTTP_ERROR_CODE, 'status code: ' . $httpClient->getStatus());
         return false;
     }
     $header = $httpClient->getHeader();
     $contentType = $header['Content-Type'][0];
     //Content-Type为text/时表示带有errcode错误信息
     if (!isset($header['Content-Type']) || stripos($contentType, 'text') !== false) {
         $result = json_decode($httpClient->getResponse(), true);
         if (!$result) {
             throw ApiException::throws(ApiException::JSON_DECODE_ERROR_CODE, 'response: ' . $httpClient->getResponse());
         }
         if (isset($result['errcode']) && $result['errcode'] != 0) {
             throw new ApiException($result['errmsg'], $result['errcode']);
             //非0状态码
         }
         return false;
     }
     //写入文件
     if (empty($toFile)) {
         if (!is_dir(RES_ROOT . '/media')) {
             mkdir(RES_ROOT . '/media');
             chmod(RES_ROOT . '/media', 0775);
         }
         $toFile = RES_ROOT . "/media/{$mediaId}" . $this->parseExtension($contentType);
     }
     file_put_contents($toFile, $httpClient->getResponse());
     return filesize($toFile);
 }
Esempio n. 4
0
 /**
  * 获取用户的openid列表(每次最多拉取10000个)
  * @param string $fromOpenId 起始openid,不填写代表接上次结果继续拉取
  * @return array 接口返回结果集合,包含总关注数、本次拉取数及openid列表
  * @throws ApiException
  */
 public function getUserList($fromOpenId = '')
 {
     if (empty($fromOpenId)) {
         $fromOpenId = $this->nextOpenId;
     }
     $url = $this->url . '/get?access_token=' . $this->accessToken . '&next_openid=' . $fromOpenId;
     $httpClient = new HttpClient($url);
     $httpClient->get();
     $result = $httpClient->jsonToArray();
     if (isset($result['total'])) {
         $this->nextOpenId = $result['next_openid'];
         if ($result['count'] < self::LIST_MAX) {
             //拉取完毕
             $this->nextOpenId = '';
         }
         return $result['data']['openid'];
     } else {
         throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse());
     }
     return false;
 }
Esempio n. 5
0
 /**
  * 查询所有分组
  * @return array 分组数组(失败时返回false)
  * @throws ApiException
  */
 public function getAll()
 {
     $httpClient = new HttpClient($this->url . '/get?access_token=' . $this->accessToken);
     $httpClient->get();
     $result = $httpClient->jsonToArray();
     if (isset($result['groups'])) {
         return $result['groups'];
     } else {
         throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse());
     }
     return false;
 }
Esempio n. 6
0
 /**
  * 删除自定义菜单(默认或个性化)
  * @param string $menuId 可选,个性化菜单的menuid,不填则删除所有菜单(包括默认和个性化)
  * @return boolean 删除成功时返回true
  * @throws ApiException
  */
 public function delete($menuId = null)
 {
     if (is_string($menuId)) {
         $url = $this->url . '/delconditional?access_token=' . $this->accessToken;
         $httpClient = new HttpClient($url);
         $httpClient->setBody(json_encode(array('menuid' => $menuId)));
         $httpClient->post();
     } else {
         $url = $this->url . '/delete?access_token=' . $this->accessToken;
         $httpClient = new HttpClient($url);
         $httpClient->get();
     }
     $result = $httpClient->jsonToArray();
     if (isset($result['errcode']) && $result['errcode'] == 0) {
         return true;
     }
     //never
     return false;
 }
Esempio n. 7
0
 /**
  * 获取在线客服接待信息
  * @return array 客服接待信息集合(失败时返回false)
  * @throws ApiException
  */
 public function getOnlineList()
 {
     $url = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=' . $this->accessToken;
     $httpClient = new HttpClient($url);
     $httpClient->get();
     $result = $httpClient->jsonToArray();
     if (isset($result['kf_online_list'])) {
         return $result['kf_online_list'];
     } else {
         throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse());
     }
     //never
     return false;
 }