WagentProxy.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/8/15
  6. * Time: 16:56
  7. */
  8. namespace App\Lib\Biz\Sport;
  9. class WagentProxy
  10. {
  11. private $token = '';
  12. private $cacheFile = '';
  13. private $domain = 'http://sports.bocai108.com';
  14. private $configs = [];
  15. public function setCacheFile($file = '')
  16. {
  17. if (empty($file)) {
  18. $file = __DIR__ . DIRECTORY_SEPARATOR . 'token.cache';
  19. }
  20. $this->cacheFile = $file;
  21. }
  22. public function getCacheFile()
  23. {
  24. if (empty($this->cacheFile)) {
  25. $this->cacheFile = __DIR__ . DIRECTORY_SEPARATOR . 'token.cache';
  26. }
  27. return $this->cacheFile;
  28. }
  29. //写入本地缓存 注意目录和文件要有与入权根
  30. public function writeTokenCache($array)
  31. {
  32. $ret = file_put_contents($this->cacheFile, json_encode($array, 256));
  33. return $ret;
  34. }
  35. //读取本地缓存
  36. public function readTokenCache()
  37. {
  38. if (!file_exists($this->cacheFile)) {
  39. return false;
  40. }
  41. $ret = file_get_contents($this->cacheFile);
  42. if ($ret) {
  43. return $ret;
  44. } else {
  45. return false;
  46. }
  47. }
  48. //请求 url地址get时可以带参数 ,param为post提交的参数数组 isPost是否是post方式提交,默认为get
  49. function request_post($url = '', $param = '', $isPost = 0)
  50. {
  51. if (empty($url)) {
  52. return false;
  53. }
  54. if ($isPost && empty($param)) {
  55. return false;
  56. }
  57. $postUrl = $url;
  58. $curlPost = $param;
  59. $ch = curl_init();//初始化curl
  60. if (!$isPost && $param) {
  61. $postUrl = $postUrl . '?' . $param;
  62. }
  63. curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
  64. curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  65. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  66. if ($isPost) {
  67. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  68. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  69. }
  70. $data = curl_exec($ch);//运行curl
  71. curl_close($ch);
  72. return $data;
  73. }
  74. //获取token
  75. protected function getToken()
  76. {
  77. $cacheData = json_decode($this->readTokenCache(), true);
  78. if ($cacheData && isset($cacheData['ctime']) && isset($cacheData['token']) && isset($cacheData['domain'])) {
  79. $ctime = intval($cacheData['ctime']);
  80. $token = $cacheData['token'];
  81. $domain = $this->domain = $cacheData['domain'];
  82. if ((time() - $ctime) >= 24 * 60 * 60 - 300) {
  83. $token = '';
  84. }
  85. if ($token) {
  86. $this->domain = $domain;
  87. $this->token = $token;
  88. return $token;
  89. }
  90. }
  91. return false;
  92. }
  93. //刷新token值
  94. public function refreshTokenData($config)
  95. {
  96. $name = isset($config['name']) ? $config['name'] : '';
  97. $key = isset($config['key']) ? $config['key'] : '';
  98. $secret = isset($config['secret']) ? $config['secret'] : '';
  99. $domain = isset($config['domain']) ? $config['domain'] : '';
  100. if (empty($name) || empty($key) || empty($secret) || empty($domain)) {
  101. throw new \Exception('require params error!', 1000);
  102. }
  103. $url = $domain . '/InApi-index/getToken';
  104. $tokenTmp = $this->request_post($url, ['wagent_name' => $name, 'wagent_key' => $key, 'wagent_secret' => $secret], 1);
  105. if ($tokenTmp) {
  106. $tokenData = json_decode($tokenTmp, true);
  107. if (isset($tokenData['status']) && $tokenData['status'] == 1 && isset($tokenData['data']['token']) && ($tokenData['data']['token'] != '')) {
  108. $this->writeTokenCache(['ctime' => time(), 'token' => $tokenData['data']['token'], 'domain' => $domain]);
  109. $this->token = $tokenData['data']['token'];
  110. return $tokenData['data']['token'];
  111. } else {
  112. return $tokenTmp;
  113. }
  114. }
  115. throw new \Exception('get Token False', 1001);
  116. }
  117. //配置
  118. public function setConfigs(Array $config)
  119. {
  120. $name = isset($config['name']) ? $config['name'] : '';
  121. $key = isset($config['key']) ? $config['key'] : '';
  122. $secret = isset($config['secret']) ? $config['secret'] : '';
  123. $domain = isset($config['domain']) ? $config['domain'] : '';
  124. $cachefile = isset($config['cachefile']) ? $config['cachefile'] : '';
  125. if (empty($name) || empty($key) || empty($secret)) {
  126. throw new \Exception('初始化参数错误,name,key,secret不能为空', 1002);
  127. }
  128. if (empty($domain)) {
  129. $domain = $this->domain;
  130. }
  131. $this->configs = ['name' => $name, 'key' => $key, 'secret' => $secret, 'domain' => $domain];
  132. $this->setCacheFile($cachefile);
  133. return true;
  134. }
  135. //生成带验证的请求url
  136. public function makeUrl(String $route, Array $paraArray, $arrayRet = 0)
  137. {
  138. $str = '';
  139. foreach ($paraArray as $nkey => $val) {
  140. $str .= "&" . $nkey . '=' . $val;
  141. }
  142. $param = base64_encode(substr($str, 1));
  143. $Key = md5($param . $this->token);
  144. if ($arrayRet) {
  145. return ['url' => $this->domain . $route, 'param' => $param, 'key' => $Key];
  146. }
  147. $url = $this->domain . $route . '?params=' . $param . '&Key=' . $Key;
  148. return $url;
  149. }
  150. //初始化操作
  151. public function Init($config)
  152. {
  153. $this->setConfigs($config);
  154. if ($this->getToken()) {
  155. return true;
  156. }
  157. $this->refreshTokenData($this->configs);
  158. return true;
  159. }
  160. /////////////////////////////////////以下为具体业务逻辑
  161. //请在调用以下方法前,调用 Init方法作初始化操作
  162. //2.1检测账户是否存在
  163. public function caie($name)
  164. {
  165. $arrcomm = ['agent' => $this->configs['name'], 'method' => 'caie'];
  166. $arrthis = ['username' => $name];
  167. $url = $this->makeUrl('/InApi-index/dobusiness', array_merge($arrcomm, $arrthis));
  168. $ret = $this->request_post($url);
  169. return $ret;
  170. }
  171. //2.2 检测并创建账号
  172. public function caca($name, $pass)
  173. {
  174. $arrcomm = ['agent' => $this->configs['name'], 'method' => 'caca'];
  175. $arrthis = ['username' => $name, 'password' => $pass];
  176. $url = $this->makeUrl('/InApi-index/dobusiness', array_merge($arrcomm, $arrthis));
  177. $ret = $this->request_post($url);
  178. return $ret;
  179. }
  180. ///2.3用户查询余额
  181. public function gb($name, $pass)
  182. {
  183. $arrcomm = ['agent' => $this->configs['name'], 'method' => 'gb'];
  184. $arrthis = ['username' => $name, 'password' => $pass];
  185. $url = $this->makeUrl('/InApi-index/dobusiness', array_merge($arrcomm, $arrthis));
  186. $ret = $this->request_post($url);
  187. return $ret;
  188. }
  189. ///2.4用户密码更改 (用post方法提交的例子,注意参数格式)
  190. public function ua($name, $pass)
  191. {
  192. $arrcomm = ['agent' => $this->configs['name'], 'method' => 'ua'];
  193. $arrthis = ['username' => $name, 'password' => $pass];
  194. $urlarr = $this->makeUrl('/InApi-index/dobusiness', array_merge($arrcomm, $arrthis), 1);
  195. $ret = $this->request_post($urlarr['url'], ['params' => $urlarr['param'], 'Key' => $urlarr['key']], 1);
  196. return $ret;
  197. }
  198. //...............
  199. }