cacheFile = $file; } public function getCacheFile() { if (empty($this->cacheFile)) { $this->cacheFile = __DIR__ . DIRECTORY_SEPARATOR . 'token.cache'; } return $this->cacheFile; } //写入本地缓存 注意目录和文件要有与入权根 public function writeTokenCache($array) { $ret = file_put_contents($this->cacheFile, json_encode($array, 256)); return $ret; } //读取本地缓存 public function readTokenCache() { if (!file_exists($this->cacheFile)) { return false; } $ret = file_get_contents($this->cacheFile); if ($ret) { return $ret; } else { return false; } } //请求 url地址get时可以带参数 ,param为post提交的参数数组 isPost是否是post方式提交,默认为get function request_post($url = '', $param = '', $isPost = 0) { if (empty($url)) { return false; } if ($isPost && empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $ch = curl_init();//初始化curl if (!$isPost && $param) { $postUrl = $postUrl . '?' . $param; } curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页 curl_setopt($ch, CURLOPT_HEADER, 0);//设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 if ($isPost) { curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); } $data = curl_exec($ch);//运行curl curl_close($ch); return $data; } //获取token protected function getToken() { $cacheData = json_decode($this->readTokenCache(), true); if ($cacheData && isset($cacheData['ctime']) && isset($cacheData['token']) && isset($cacheData['domain'])) { $ctime = intval($cacheData['ctime']); $token = $cacheData['token']; $domain = $this->domain = $cacheData['domain']; if ((time() - $ctime) >= 24 * 60 * 60 - 300) { $token = ''; } if ($token) { $this->domain = $domain; $this->token = $token; return $token; } } return false; } //刷新token值 public function refreshTokenData($config) { $name = isset($config['name']) ? $config['name'] : ''; $key = isset($config['key']) ? $config['key'] : ''; $secret = isset($config['secret']) ? $config['secret'] : ''; $domain = isset($config['domain']) ? $config['domain'] : ''; if (empty($name) || empty($key) || empty($secret) || empty($domain)) { throw new \Exception('require params error!', 1000); } $url = $domain . '/InApi-index/getToken'; $tokenTmp = $this->request_post($url, ['wagent_name' => $name, 'wagent_key' => $key, 'wagent_secret' => $secret], 1); if ($tokenTmp) { $tokenData = json_decode($tokenTmp, true); if (isset($tokenData['status']) && $tokenData['status'] == 1 && isset($tokenData['data']['token']) && ($tokenData['data']['token'] != '')) { $this->writeTokenCache(['ctime' => time(), 'token' => $tokenData['data']['token'], 'domain' => $domain]); $this->token = $tokenData['data']['token']; return $tokenData['data']['token']; } else { return $tokenTmp; } } throw new \Exception('get Token False', 1001); } //配置 public function setConfigs(Array $config) { $name = isset($config['name']) ? $config['name'] : ''; $key = isset($config['key']) ? $config['key'] : ''; $secret = isset($config['secret']) ? $config['secret'] : ''; $domain = isset($config['domain']) ? $config['domain'] : ''; $cachefile = isset($config['cachefile']) ? $config['cachefile'] : ''; if (empty($name) || empty($key) || empty($secret)) { throw new \Exception('初始化参数错误,name,key,secret不能为空', 1002); } if (empty($domain)) { $domain = $this->domain; } $this->configs = ['name' => $name, 'key' => $key, 'secret' => $secret, 'domain' => $domain]; $this->setCacheFile($cachefile); return true; } //生成带验证的请求url public function makeUrl(String $route, Array $paraArray, $arrayRet = 0) { $str = ''; foreach ($paraArray as $nkey => $val) { $str .= "&" . $nkey . '=' . $val; } $param = base64_encode(substr($str, 1)); $Key = md5($param . $this->token); if ($arrayRet) { return ['url' => $this->domain . $route, 'param' => $param, 'key' => $Key]; } $url = $this->domain . $route . '?params=' . $param . '&Key=' . $Key; return $url; } //初始化操作 public function Init($config) { $this->setConfigs($config); if ($this->getToken()) { return true; } $this->refreshTokenData($this->configs); return true; } /////////////////////////////////////以下为具体业务逻辑 //请在调用以下方法前,调用 Init方法作初始化操作 //2.1检测账户是否存在 public function caie($name) { $arrcomm = ['agent' => $this->configs['name'], 'method' => 'caie']; $arrthis = ['username' => $name]; $url = $this->makeUrl('/InApi-index/dobusiness', array_merge($arrcomm, $arrthis)); $ret = $this->request_post($url); return $ret; } //2.2 检测并创建账号 public function caca($name, $pass) { $arrcomm = ['agent' => $this->configs['name'], 'method' => 'caca']; $arrthis = ['username' => $name, 'password' => $pass]; $url = $this->makeUrl('/InApi-index/dobusiness', array_merge($arrcomm, $arrthis)); $ret = $this->request_post($url); return $ret; } ///2.3用户查询余额 public function gb($name, $pass) { $arrcomm = ['agent' => $this->configs['name'], 'method' => 'gb']; $arrthis = ['username' => $name, 'password' => $pass]; $url = $this->makeUrl('/InApi-index/dobusiness', array_merge($arrcomm, $arrthis)); $ret = $this->request_post($url); return $ret; } ///2.4用户密码更改 (用post方法提交的例子,注意参数格式) public function ua($name, $pass) { $arrcomm = ['agent' => $this->configs['name'], 'method' => 'ua']; $arrthis = ['username' => $name, 'password' => $pass]; $urlarr = $this->makeUrl('/InApi-index/dobusiness', array_merge($arrcomm, $arrthis), 1); $ret = $this->request_post($urlarr['url'], ['params' => $urlarr['param'], 'Key' => $urlarr['key']], 1); return $ret; } //............... }