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

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

    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美团大众点评团购券内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

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

相关文章

laravel与thinkphp之间的区别与优缺点

目录Laravel框架:问题描述:1、渲染模版方式的不同2、在实际开发中我们常常遇到这样的问题,就是开发地点不固定。3、Laravel框架提供了大量的闭包4、post传值中注意点不同5、条件判断语句书...

php中echo、print和print_r的区别点及用法总结

本教程操作环境:windows10系统、PHP7.1版,DELL G3电脑php中echo、print和print_r的区别是什么echo是PHP语句, print和print_r是函数,语句没有返回...

php png失真的原因及解决办法

1、创建一个PHP示例文件。2、创建一个和背景图片一样大小的真彩色画布。3、复制背景图片。4、通过“imagecreatefrompng”合成png图片即可。实例?123456...

浅谈PHP7中的一些小技巧

目录相同的命名空间,相同的 use常量可以是数组太空船操作符数组的第一 / 最后一个键 (PHP 7.3)数组延展操作符 (PHP 7.4)箭头函数 (PHP 7.4)常量的可见性 (PHP7.1)C...

国外多IP服务器如何选择国外多ip服务器租用地址是多少

一、国外多IP服务器有什么用?1、容易被搜索引擎收录普通国外服务器可能只包含一个独立IP。如果很多网站都放在一个IP上,那么引擎就不会重视,甚至会被视为毫无价值的网站。国外多IP服务器提供多个独立IP...

PHP导出数据超时的优化建议

一般情况下,导出超时可能都是以下三种情况:  一、sql语句复杂,查询时间过长;  二、处理查询后数据逻辑冗余;  三、数据量过大导致响应超时。接下来分别给出这三种情况的优化建议。一、sql语句复杂,...