PHP 对接美团大众点评团购券(门票)的开发步骤

编辑: admin 分类: php 发布时间: 2021-11-24 来源:互联网

一、功能简要介绍

    1、根据需求,用户在美团大众点评中所购买的门票在自己的系统上可以核销,同时把核销信息存储到自己的系统里。

   2、美团点评API文档地址:https://open.dianping.com/document/v2?rootDocId=5000

二、开发步骤:

   1、用点评管家账号登录文档,相应文档说明:https://open.dianping.com/document/v2?docId=6000136&rootDocId=1000

  2 、选好自己相应的应用型,审核通过之后,就可以用平台系统的测试的数据就行接口调用

3、在大众点评app购买相应票,根据验券核销API文档(根据业务需求选择的类型是自用型)调取查询券、验券即核销券的接口,代码如下:

//查询券、验券
 public function prepare(){
  // $arr 为数组 里面为应用参数
  $appKey = "1ef04****e3a8"; //key
  $secret = "dd8a291f9f*****4e3fbd9be6ff"; //秘钥
  $timestamp = date('Y-m-d H:i:s');
  $format = 'json';
  $v = 1;
  $sign_method = 'MD5';
  $qr_code = '257***4228';
  $open_shop_uuid = '34d5902420ca****48e941a38d773';
  $session = 'bbf7059256aaf3f1****3b4d9c0816cf819b7';
 
  $data = [
   'app_key' => $appKey,
   'timestamp' => $timestamp,
   'sign_method' => $sign_method,
   'format' => $format,
   'v' => $v,
   'session' => $session,
  ];
  $arr = [
   'requestid'=>'123',
   'receipt_code' => $qr_code,
   //'qr_code'=>$qr_code,
   'open_shop_uuid' => $open_shop_uuid,
   //'count'=>1,
   //'app_shop_account' =>'test',
   // 'app_shop_accountname' =>'test1',
  ];
  $data = array_merge($data, $arr);
  ksort($data);
  $sign = $this->cal_sign($secret, $data);//获取签名
  $data['sign'] = $sign;
//  $data['count'] = $sign;
//  $data['app_shop_account'] = $sign;
//  $data['app_shop_accountname'] = $sign;
  $data = array_merge($data, $arr);
  $postdata = http_build_query($data);
  $url = 'https://openapi.dianping.com/router/tuangou/receipt/prepare';//输码查询券
  //$url = 'https://openapi.dianping.com/router/tuangou/receipt/scanprepare';//扫码查询券
  //$url = 'https://openapi.dianping.com/router/tuangou/receipt/consume';//验券
  $tmpInfo=$this->curl_post($url,$postdata);
  var_dump($tmpInfo);
 }
 
 
 
/**
  * 计算签名
  *
  * @param $app_secret 三方app_secret
  * @param $req_param 请求参数集合,包括公共参数和业务参数
  * @return string md5签名
  */
 function cal_sign($app_secret, $req_param)
 {
  // 排序所有请求参数
  ksort($req_param);
  $src_value = "";
  // 按照key1value1key2value2...keynvaluen拼接
  foreach ($req_param as $key => $value) {
   $src_value .= ($key . $value);
  }
  //计算md5
  return md5($app_secret . $src_value . $app_secret);
 }
 
 //post请求
  private 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_POSTFI【本文来源:迪拜服务器 转载请说明出处】ELDS, $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;
 }

运用平台提供的相应参数,流程能顺利走通。

4、当放到线上的时候,选择相应的进行上线,首先要去平台“我的应用”设置回调地址,然后再授权地址上加上回调地址,还要进行授权去获取session,然后通过session去获取店铺的id

回调地址:

授权地址:

5、授权地址:

https://e.dianping.com/dz-open/merchant/auth?app_key=1ef0*****e3a8&redirect_url=https://*****/admin/Dian/get_auth&state=teststate&scope=[%22tuangou%22]

授权地址可以获取auth_code,根据这个值去换取session,换取session后可以去换取店铺id

//获取auth_code
 
 public function get_auth(){
  $auth_code = input('auth_code');
  if(!isset($auth_code)) {
   $app_key='1ef*****3a8';
   $state='teststate';
   $redirect_url='https://****/admin/Dian/get_auth';
   $scope='tuangou';
   $url='https://e.dianping.com/dz-open/merchant/auth?';
   $data=[
    'app_key' =>$app_key,
    'state' => $state,
    'redirect_url' =>$redirect_url,
    'scope' =>$scope,
   ];
   $postdata = http_build_query($data);
   Header("Location: $url$postdata");
  } else {
   var_dump($auth_code);die();
  }
 }
 
 
 //获取session
 public function get_session(){
  $app_key='1ef0*****e3a8';
  $app_secret='dd8a291******bd9be6ff';
  $auth_code='4e027519e******a6a9d1ee5f2';
  $grant_type='authorization_code';
  $redirect_url='https://*****/admin/Dian/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);
  var_dump($tmpInfo);
 }
 
 
//获取所有店铺的id
 public function get_shopid(){
  $app_key='1ef04*****e3a8';
  $secret = "dd8a29*****fbd9be6ff"; //秘钥
  $sign_method='MD5';
  $timestamp = date('Y-m-d H:i:s');
  $format = 'json';
  $v = 1;
  //$session = 'f44d594ab895c******85b70bade02c';
  $session = 'bbf7059256aaf3*******0816cf819b7';
  $bid='5da1aab********5ad457a2c';//和session一起返回的
  $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->cal_sign($secret, $data);
  $data['sign'] = $sign;
  $postdata = http_build_query($data);
  $tmpInfo=$this->curl_get($url.$postdata);
  var_dump($tmpInfo);
 }

到此这篇关于PHP 对接美团大众点评团购券(门票)的开发步骤的文章就介绍到这了,更多相关PHP美团大众点评团购券内容请搜索海外IDC网以前的文章或继续浏览下面的相关文章希望大家以后多多支持海外IDC网!