Huafei.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\controller\api\store\service;
  3. use think\App;
  4. use crmeb\basic\BaseController;
  5. class Huafei extends BaseController
  6. {
  7. public $appkey = '1077740514';
  8. public $secretKey = 'f154a106d56d804fcc640d9da4844b0c';
  9. // 生产
  10. public $gatewayUrl = "https://router.wikeyun.cn";
  11. // 备用
  12. // public $gatewayUrl = "https://router.wikeyun.cn/rest";
  13. public $format = "json";
  14. /** 是否打开入参check**/
  15. protected $signMethod = "md5";
  16. protected $apiVersion = "1.0";
  17. public function __construct(App $app)
  18. {
  19. parent::__construct($app);
  20. }
  21. //充值话费推送
  22. public function recharge(){
  23. $request['store_id'] = '9189';
  24. $request['mobile'] = '18973958920';
  25. $request['money'] = 100;
  26. $request['recharge_type'] = 1;
  27. $request['notify_url'] = 'https://api.gdjbp.com//api/huafei/notify';
  28. $arr = $this->execute('/rest/Recharge/PushOrder', $request);
  29. return app('json')->success('话费推送成功', $arr);
  30. }
  31. protected function generateSign($params){
  32. ksort($params);
  33. $stringToBeSigned = $this->secretKey;
  34. foreach ($params as $k => $v)
  35. {
  36. if (is_array($v)) {
  37. $stringToBeSigned .= $k;
  38. }else{
  39. if ("@" != substr($v, 0, 1)) {
  40. $stringToBeSigned .= "$k$v";
  41. }
  42. }
  43. }
  44. unset($k, $v);
  45. $stringToBeSigned .= $this->secretKey;
  46. return strtoupper(md5($stringToBeSigned));
  47. }
  48. public function curl($url, $postFields = null,$timeOut = 20){
  49. $ch = curl_init();
  50. curl_setopt($ch, CURLOPT_URL, $url);
  51. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  52. curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com');
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  55. //https 请求
  56. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  58. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  59. }
  60. if (is_array($postFields) && 0 < count($postFields)){
  61. $postBodyString = "";
  62. $postMultipart = false;
  63. foreach ($postFields as $k => $v){
  64. if("@" != substr($v, 0, 1))//判断是不是文件上传
  65. {
  66. $postBodyString .= "$k=" . urlencode($v) . "&";
  67. }
  68. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  69. {
  70. $postMultipart = true;
  71. }
  72. $postMultipart = true;
  73. }
  74. unset($k, $v);
  75. curl_setopt($ch, CURLOPT_POST, true);
  76. if ($postMultipart){
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  78. }else{
  79. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  80. }
  81. }
  82. $reponse = curl_exec($ch);
  83. curl_close($ch);
  84. return json_decode($reponse, true);
  85. }
  86. /**
  87. * request api接口后缀,如 /recharge/pushOrder
  88. * apiParams 业务参数 数组
  89. */
  90. public function execute($request, $apiParams ,$timeOut =20){
  91. //组装系统参数
  92. $sysParams["app_key"] = $this->appkey;
  93. $sysParams["v"] = $this->apiVersion;
  94. $sysParams["format"] = $this->format;
  95. $sysParams["timestamp"] = time();
  96. $sysParams["client"] = '';
  97. //签名
  98. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  99. //系统参数放入GET请求串
  100. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  101. $requestUrl = $this->gatewayUrl.$request. "?";
  102. foreach ($sysParams as $sysParamKey => $sysParamValue){
  103. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  104. }
  105. $requestUrl = substr($requestUrl, 0, -1);
  106. return $this->curl($requestUrl, $apiParams ,$timeOut);
  107. }
  108. }