PHP Thinkphp5.0对接美团API接口 实现卡卷核销功能
短信预约 -IT技能 免费直播动态提醒
好久没有更新文章了。不知道关注我的小伙伴们是不是已经不在关注我了。最近在忙着一些推广运营的事情,所以没有更新,
这几天接到一个项目,需要对接美团API接口,实现核销功能。现在就来给大家展示具体代码,
public function tuangou_hexiao(){ // $qr_cpde = $_GET['qr_code']; $open_shop_uuid = $_GET['open_shop_uuid']; // $arr 为数组 里面为应用参数 $appKey = "111111111"; $secret = "11111111111111111"; //秘钥 $timestamp = date('Y-m-d H:i:s'); $format = 'json'; $v = 1; $sign_method = 'MD5'; //因为功能要实现在小程序,所以获取的必要信息要存在文件中,有必要时拿出来 //不是必要行为, $file = $this->readFile(); $data = [ 'app_key' => $appKey, 'timestamp' => $timestamp, 'sign_method' => $sign_method, 'format' => $format, 'v' => $v, //此处的session值,我原本是存在文件中的, //如果你不需要存文件,那你就当做参数传递过来 'session' => $file['session'], ]; //根据不同的操作类型配置不同的参数 //具体参数含义见 https://open.dianping.com/document/v2?docId=6000176&rootDocId=5000 if($_GET['type']=='save'){ $arr = [ 'requestid' => '123', 'count'=>1, 'receipt_code' => $qr_cpde, 'open_shop_uuid' => $open_shop_uuid, 'app_shop_account' => '账号', 'app_shop_accountname' => '账号名称', ]; $url = 'https://openapi.dianping.com/router/tuangou/receipt/consume';//验券(核销) }elseif ($_GET['type']=='cancel'){ $arr = [ 'app_deal_id' => $_GET['deal_id'], 'receipt_code' => $qr_cpde, 'open_shop_uuid' => $open_shop_uuid, 'app_shop_account' => '账号', 'app_shop_accountname' => '账号名称', ]; $url = 'https://openapi.dianping.com/router/tuangou/receipt/reverseconsume';//撤销 }else{ $arr = [ 'receipt_code' => $qr_cpde, 'open_shop_uuid' => $open_shop_uuid, ]; $url = 'https://openapi.dianping.com/router/tuangou/receipt/prepare';//查询券 } $data = array_merge($data, $arr); ksort($data); $sign = $this->call_sign($secret, $data);//获取签名 $data['sign'] = $sign; $data = array_merge($data, $arr); $postdata = http_build_query($data); $tmpInfo= $this->curl_post($url,$postdata); $this->ReturnSucess($tmpInfo); } public function get_auth(){ $auth_code = $_GET["auth_code"]; //判断是否为回调 if(empty($auth_code)) { $app_key='11111111111'; $state='teststate'; //回调的url,我配置为当前方法。 $redirect_url='https:你的路径/index/get_auth'; $scope='tuangou'; $url='https://e.dianping.com/dz-open/merchant/auth?'; $data=[ 'app_key' =>$app_key, 'state' => $state, 'redirect_url'=>$redirect_url ]; $postdata = http_build_query($data); header("Location: $url$postdata"); } else { trace('回调成功'.$auth_code); //根据auth_code 获取session的授权码 $tmpInfo = $this->get_session($auth_code); //根据$session $bid 获取店铺id $shopInfo = $this->get_shopid($tmpInfo['access_token'],$tmpInfo['bid']); } } //授权获取session public function get_session($auth_code){ $app_key='1111111'; $app_secret='111111111111111111111'; $grant_type='authorization_code'; $redirect_url='https:你的路径/index/get_auth'; $data=[ 'app_key' =>$app_key, 'app_secret' => $app_secret, 'redirect_url' =>$redirect_url, 'auth_code' =>$auth_code, 'grant_type' =>$grant_type ]; $postdata = http_build_query($data); $url='https://openapi.dianping.com/router/oauth/token'; $tmpInfo=$this->curl_post($url,$postdata); return $tmpInfo; } //获取所有店铺的id public function get_shopid($session,$bid){ $app_key='11111111'; $secret = "111111111111111111111111"; //秘钥 $sign_method='MD5'; $timestamp = date('Y-m-d H:i:s'); $format = 'json'; $v = 1; $offset =0; $limit = 20; $url='https://openapi.dianping.com/router/oauth/session/scope?'; $data=[ 'app_key' =>$app_key, 'sign_method' => $sign_method, 'timestamp' =>$timestamp, 'format' =>$format, 'v' =>$v, 'session' =>$session, 'bid' =>$bid, 'offset' =>$offset, 'limit' =>$limit, ]; ksort($data); $sign = $this->call_sign($secret, $data); $data['sign'] = $sign; $postdata = http_build_query($data); $tmpInfo=$this->curl_get($url.$postdata); return $tmpInfo; } public function createFile($data){ //创建一个文件 $file = fopen('MeiTuan.txt','w'); //写入文件 fwrite($file, json_encode($data)); //关闭文件 fclose($file); } public function readFile(){ $file = fopen('MeiTuan.txt','r'); $data = fread( $file , filesize('MeiTuan.txt') ); fclose($file); return json_decode( $data , true ); } function call_sign($secret, $data) { // 排序所有请求参数 ksort($data); $class="lazy" data-src_value = ""; // 按照key1value1key2value2...keynvaluen拼接 foreach ($data as $key => $value) { $class="lazy" data-src_value .= ($key . $value); } //计算md5 return md5($secret . $class="lazy" data-src_value . $secret); } //post请求 function curl_post($url,$postdata){ $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测 curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Expect:' )); // 解决数据包大不能提交 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 $tmpInfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno' . curl_error($curl); } curl_close($curl); // 关键CURL会话 $tmpInfo=json_decode($tmpInfo,true); return $tmpInfo; } //get请求 private function curl_get($url) { //初使化curl $curl = curl_init(); //请求的url,由形参传入 curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测 curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Expect:' )); // 解决数据包大不能提交 //将得到的数据返回 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //不处理头信息 curl_setopt($curl, CURLOPT_HEADER, 0); //连接超过10秒超时 curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 //执行curl $output = curl_exec($curl); if (curl_errno($curl)) { echo 'Errno' . curl_error($curl); } //关闭资源 curl_close($curl); //返回内容 $tmpInfo=json_decode($output,true); return $tmpInfo; }}
这段代码是实现美团核销的功能。 亲测有效,如果有小伙伴需要的话,可否动动你的手指,给我点个关注呢。
来源地址:https://blog.csdn.net/weixin_47736740/article/details/132087655
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341