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

admin3年前PHP教程95
一、功能简要介绍

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

   2、美团点评api文档地址:open.dianping/document/v2?rootdocid=5000

二、开发步骤:

   1、用点评管家账号登录文档,相应文档说明:open.dianping/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 = 'openapi.dianping/router/tuangou/receipt/prepare';//输码查询券
  //$url = 'openapi.dianping/router/tuangou/receipt/scanprepare';//扫码查询券
  //$url = 'openapi.dianping/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_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;
 }


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

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

回调地址:

授权地址:

5、授权地址:

e.dianping/dz-open/merchant/auth?app_key=1ef0*****e3a8&redirect_url=*****/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='****/admin/dian/get_auth';
   $scope='tuangou';
   $url='e.dianping/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='*****/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='openapi.dianping/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='openapi.dianping/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美团大众点评团购券内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

在PHP中灵活使用foreach+list处理多维数组的方法

先抛出问题,有时候我们接收到的参数是多维数组,我们需要将他们转成普通的数组,比如:$arr = [  [1, 2, [3, 4]],  [5, 6, [7, 8...

新加坡服务器延迟大怎么解决

新加坡服务器延迟大怎么解决?如果你使用的是网络游戏或视频会议等需要低延迟的应用,而在连接新加坡服务器时遇到了延迟问题,以下是一些可能有助于解决延迟问题的方法:更换ISP(互联网服务提供商):你可以尝试...

游戏服务器需要配置显卡吗新加坡显卡服务器租用需要注意的有哪些

游戏服务器需要配置显卡吗?游戏行业一直以来对服务器的要求相对较高,无论是从网络延迟还是稳定性都有严格的要求,今天我就来介绍一些关于游戏服务器所需要的条件。1.处理器性能要高高速处理器:整个服务器中的重...

马来西亚服务器租用哪家好?

马来西亚服务器租用哪家好?在选择一家好的马来西亚服务器提供商时,你应该考虑以下几个方面:可靠性和稳定性:你需要确保所选择的服务商提供高质量、可靠的硬件和网络基础设施,以确保你的网站或应用程序的稳定性和...

PHP输出缓冲与header发送问题详解

如果你在header或cookie函数前发送大量字符到浏览器,就会报headers already sent  错误,以下为说明:headers_sent()此函数告诉我们发送header的...

日本服务器可以用来做什么

日本服务器可以用来做什么?日本服务器可以用于各种不同的目的,例如:网站托管:您可以将自己的网站托管在日本服务器上,以提高网站的访问速度和性能,并为用户提供更好的使用体验。游戏服务器:日本是一个游戏文化...