1
0

Luban.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace call;
  3. class Luban
  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. * @return Array
  19. */
  20. public function bind($caller, $callee, $telX)
  21. {
  22. $url = 'http://open.hnbaoze.cn/10086/call/';
  23. $param = [
  24. 'types' => 'callout',
  25. 'appid' => $this->appid,
  26. 'caller' => $caller,
  27. 'callees' => $callee,
  28. 'bindnum' => $telX
  29. ];
  30. $param['sign'] = $this->getSigin($param);
  31. $rs = $this->curl($url, $param);
  32. return json_decode($rs, true);
  33. }
  34. /**
  35. * 获取签名
  36. *
  37. * @param Array $params
  38. *
  39. *
  40. * 签名算法:
  41. * 1,将appid的值先sha256加密,再base64加密
  42. * 2,与appkey值拼接得到sign完整签名
  43. *
  44. *
  45. * @return String
  46. */
  47. public function getSigin($params)
  48. {
  49. return base64_encode(hash('sha256', $this->appid)).$this->secretKey;
  50. }
  51. /**
  52. * curl请求
  53. *
  54. * @param String $url
  55. * @param Array $param
  56. * @return String
  57. */
  58. public function curl($url, $param)
  59. {
  60. $curl = curl_init();
  61. curl_setopt($curl, CURLOPT_URL, $url);
  62. curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type: application/json; encoding=utf-8']);
  63. if (!empty($param)) {
  64. curl_setopt($curl, CURLOPT_POST, 1);
  65. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($param));
  66. }
  67. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  68. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  69. curl_setopt($curl, CURLOPT_HEADER, false);
  70. $rs = curl_exec($curl);
  71. curl_close($curl);
  72. return $rs;
  73. }
  74. }