1
0

Yhuatong.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace call;
  3. class Yhuatong
  4. {
  5. protected $appid;
  6. protected $secretKey;
  7. public function __construct($appid, $secretKey)
  8. {
  9. $this->appid = $appid;
  10. $this->secretKey = $secretKey;
  11. }
  12. /**
  13. * 主叫与被叫绑定(路线一)
  14. *
  15. * @param String $caller 主叫
  16. * @param String $callee 被叫
  17. * @param String $telX 中间号
  18. * @param String $industry 行业id
  19. * @return Array
  20. */
  21. public function bind($caller, $callee, $telX, $industry)
  22. {
  23. $url = 'http://open-api.yhuatong.vip/open/bind';
  24. $param = [
  25. 'appid' => $this->appid,
  26. 'caller' => $caller,
  27. 'callee' => $callee,
  28. 'tel_x' => $telX,
  29. 'ts' => time(),
  30. 'industry' => $industry
  31. ];
  32. $param['sign'] = $this->getSigin($param);
  33. $rs = $this->curl($url, $param);
  34. return json_decode($rs, true);
  35. }
  36. /**
  37. * 主叫和被交绑定(混合路线)
  38. *
  39. * @param String $caller 主叫
  40. * @param String $callee 被叫
  41. * @return Array
  42. */
  43. public function bindv2($caller, $callee, $tel_x = null, $industry_id = null, $expiration = 0)
  44. {
  45. $url = 'http://open-api.yhuatong.vip/open/v2/bind';
  46. $domain = request()->domain();
  47. $param = [
  48. 'appid' => $this->appid,
  49. 'callback_url' => $domain . '/api/outCall/callBack',
  50. 'callee' => $callee,
  51. 'caller' => $caller
  52. ];
  53. if ($expiration > 0) $param['expiration'] = $expiration;
  54. // if ($tel_x === null) {
  55. $param['industry_id'] = '2golZ';
  56. // if ($industry_id !== null) {
  57. // $param['industry_id'] = $industry_id;
  58. // } else {
  59. // $industry = $this->comIndustryList();
  60. // $param['industry_id'] = $industry['data'][0]['industry_id'];
  61. // }
  62. // } else {trace('线路1');
  63. // $param['tel_x'] = $tel_x;
  64. // }
  65. $param['ts'] = time();
  66. $param['sign'] = $this->getSigin($param);
  67. $rs = $this->curl($url, $param);
  68. return json_decode($rs, true);
  69. }
  70. /**
  71. * 获取行业列表
  72. *
  73. * @return Array
  74. */
  75. public function comIndustryList()
  76. {
  77. $url = 'http://open-api.yhuatong.vip/open/comIndustryList';
  78. $param = [
  79. 'appid' => $this->appid,
  80. 'ts' => time()
  81. ];
  82. $param['sign'] = $this->getSigin($param);
  83. $rs = $this->curl($url, $param);
  84. return json_decode($rs, true);
  85. }
  86. /**
  87. * 添加白名单(加白成功会扣除10元号码费)
  88. *
  89. * @param String $phone 电话号码
  90. * @param String $realName 真实姓名
  91. * @param String $idCard 身份证号
  92. * @param String $industryId 所属行业
  93. * @return Array
  94. */
  95. public function whiteAdd($phone, $realName, $idCard, $industryId)
  96. {
  97. $url = 'http://open-api.yhuatong.vip/open/v2/white/add';
  98. $param = [
  99. 'appid' => $this->appid,
  100. 'ts' => time(),
  101. 'phone' => $phone,
  102. 'real_name' => $realName,
  103. 'id_card' => $idCard,
  104. 'industry_id' => $industryId
  105. ];
  106. $param['sign'] = $this->getSigin($param);
  107. $rs = $this->curl($url, $param);
  108. return json_decode($rs, true);
  109. }
  110. /**
  111. * 查询号码加白状态
  112. *
  113. * @param String $phone
  114. * @param String $industryId
  115. * @return Array
  116. */
  117. public function whiteShow($phone, $industryId='fe10aaab3d694d5d8931feddcde19b73')
  118. {
  119. $url = 'http://open-api.yhuatong.vip/open/v2/white/show';
  120. $param = [
  121. 'appid' => $this->appid,
  122. 'ts' => time(),
  123. 'phone' => $phone,
  124. 'industry_id' => $industryId
  125. ];
  126. $param['sign'] = $this->getSigin($param);
  127. $rs = $this->curl($url, $param);
  128. return json_decode($rs, true);
  129. }
  130. /**
  131. * 加白退网
  132. *
  133. * @param String $phones 手机号,可以多个,‘,’分割
  134. * @param String $industryId 行业/公司id
  135. * @return Array
  136. */
  137. public function whiteQuit($phones, $industryId)
  138. {
  139. $url = 'http://open-api.yhuatong.vip/open/v2/white/quit';
  140. $param = [
  141. 'appid' => $this->appid,
  142. 'ts' => time(),
  143. 'phone' => $phones,
  144. 'industry_id' => $industryId
  145. ];
  146. $param['sign'] = $this->getSigin($param);
  147. $rs = $this->curl($url, $param);
  148. return json_decode($rs, true);
  149. }
  150. /**
  151. * 余额查询
  152. *
  153. * @return void
  154. */
  155. public function balance()
  156. {
  157. $url = 'http://open-api.yhuatong.vip/open/balance';
  158. $param = [
  159. 'appid' => $this->appid,
  160. 'ts' => time()
  161. ];
  162. $param['sign'] = $this->getSigin($param);
  163. $rs = $this->curl($url, $param);
  164. return json_decode($rs, true);
  165. }
  166. /**
  167. * 根据sessionId查询通话记录
  168. *
  169. * @param String $sessionId
  170. * @return Array
  171. */
  172. public function record($sessionId)
  173. {
  174. $url = 'http://open-api.yhuatong.vip/open/record';
  175. $param = [
  176. 'appid' => $this->appid,
  177. 'sessionId' => $sessionId,
  178. 'ts' => time()
  179. ];
  180. $param['sign'] = $this->getSigin($param);
  181. $rs = $this->curl($url, $param);
  182. return json_decode($rs, true);
  183. }
  184. /**
  185. * 获取签名
  186. *
  187. * @param Array $params
  188. * @return String
  189. */
  190. public function getSigin($params)
  191. {
  192. ksort($params);
  193. $signKey = [];
  194. foreach ($params as $k => $v) {
  195. $signKey[] = $k . '=' . $v;
  196. }
  197. $signKey = implode('&', $signKey);
  198. $signKey = strtolower($signKey);
  199. $signKey .= "&appsecret=" . $this->secretKey;
  200. $md5 = md5($signKey);
  201. return $md5;
  202. }
  203. /**
  204. * curl请求
  205. *
  206. * @param String $url
  207. * @param Array $param
  208. * @return String
  209. */
  210. public function curl($url, $param)
  211. {
  212. $curl = curl_init();
  213. curl_setopt($curl, CURLOPT_URL, $url);
  214. curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type: application/json; encoding=utf-8']);
  215. if (!empty($param)) {
  216. curl_setopt($curl, CURLOPT_POST, 1);
  217. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($param));
  218. }
  219. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  220. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  221. curl_setopt($curl, CURLOPT_HEADER, false);
  222. $rs = curl_exec($curl);
  223. curl_close($curl);
  224. return $rs;
  225. }
  226. /**
  227. * 号段判断(移动联通 true, 其他false)
  228. * 移动的号段:134、135、136、137、138、139、147、150、151、152、157、158、159、172、178、 182 、183 、184 、187、188、198;
  229. * 联通的号段:130、131、132、155、156、166、185、186、145、176;
  230. * 电信的号段:133、153、177、173、180、181、189、199
  231. * 广电 :192
  232. */
  233. public function callerType($caller)
  234. {
  235. $a = ['133', '153', '177', '173', '180', '181', '189', '199', '192'];
  236. $h = substr($caller, 0, 3);
  237. if (in_array($h, $a)) return false;
  238. return true;
  239. }
  240. }