| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace app\controller\api\store\service;
- use think\App;
- use crmeb\basic\BaseController;
- class Huafei extends BaseController
- {
- public $appkey = '1077740514';
- public $secretKey = 'f154a106d56d804fcc640d9da4844b0c';
- // 生产
- public $gatewayUrl = "https://router.wikeyun.cn";
- // 备用
- // public $gatewayUrl = "https://router.wikeyun.cn/rest";
- public $format = "json";
- /** 是否打开入参check**/
- protected $signMethod = "md5";
- protected $apiVersion = "1.0";
- public function __construct(App $app)
- {
- parent::__construct($app);
- }
- //充值话费推送
- public function recharge(){
- $request['store_id'] = '9189';
- $request['mobile'] = '18973958920';
- $request['money'] = 100;
- $request['recharge_type'] = 1;
- $request['notify_url'] = 'https://api.gdjbp.com//api/huafei/notify';
- $arr = $this->execute('/rest/Recharge/PushOrder', $request);
- return app('json')->success('话费推送成功', $arr);
- }
- protected function generateSign($params){
- ksort($params);
- $stringToBeSigned = $this->secretKey;
- foreach ($params as $k => $v)
- {
- if (is_array($v)) {
- $stringToBeSigned .= $k;
- }else{
- if ("@" != substr($v, 0, 1)) {
- $stringToBeSigned .= "$k$v";
- }
- }
- }
- unset($k, $v);
- $stringToBeSigned .= $this->secretKey;
- return strtoupper(md5($stringToBeSigned));
- }
- public function curl($url, $postFields = null,$timeOut = 20){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_FAILONERROR, false);
- curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
- //https 请求
- if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- if (is_array($postFields) && 0 < count($postFields)){
- $postBodyString = "";
- $postMultipart = false;
- foreach ($postFields as $k => $v){
- if("@" != substr($v, 0, 1))//判断是不是文件上传
- {
- $postBodyString .= "$k=" . urlencode($v) . "&";
- }
- else//文件上传用multipart/form-data,否则用www-form-urlencoded
- {
- $postMultipart = true;
- }
- $postMultipart = true;
- }
- unset($k, $v);
- curl_setopt($ch, CURLOPT_POST, true);
- if ($postMultipart){
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
- }else{
- curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
- }
- }
- $reponse = curl_exec($ch);
- curl_close($ch);
- return json_decode($reponse, true);
- }
- /**
- * request api接口后缀,如 /recharge/pushOrder
- * apiParams 业务参数 数组
- */
- public function execute($request, $apiParams ,$timeOut =20){
- //组装系统参数
- $sysParams["app_key"] = $this->appkey;
- $sysParams["v"] = $this->apiVersion;
- $sysParams["format"] = $this->format;
- $sysParams["timestamp"] = time();
- $sysParams["client"] = '';
- //签名
- $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
- //系统参数放入GET请求串
- //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
- $requestUrl = $this->gatewayUrl.$request. "?";
- foreach ($sysParams as $sysParamKey => $sysParamValue){
- $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
- }
- $requestUrl = substr($requestUrl, 0, -1);
- return $this->curl($requestUrl, $apiParams ,$timeOut);
- }
- }
|