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

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

    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中的错误及其处理机制

目录什么是错误?Fatal Error:致命错误(脚本终止运行)Parse Error:编译时解析错误,语法错误(脚本终止运行)Warning Error:警告错误(仅给出提示信息,脚本不终止运行)N...

PHP实现创建以太坊钱包转账等功能

1.Geth下载(对以太坊了解不深的可以后面自己查找有关geth的资料,与此类似的客户端还有Ethereum、Parity 、Mist),本文采用windows版(Windows系统:geth-win...

PHP加MySQL消息队列深入理解

消息队列:在消息的传输过程中保存消息的容器。消息队列管理器在将消息从它的源中继到它的目标时充当中间人。队列的主要目的是提供路由并保证消息的传递;如果发送消息时接收者不可用,消息队列会保留消息,直到可以...

PHP之修改php.ini文件上传大小的配置问题案例讲解

php.iniphp.ini文件保存了php的一些重要属性,例如现在要说的文件上传的一些限制条件,关于php.ini,有一篇很好的介绍:php.ini文件详解,在windows系统中编写php的时候,...

php慢查询日志和错误日志使用详解

前言作为一名程序员,比码代码还重要那么一点点的东西就是日志的分析和查询。下面列出常见日志及设置方法。php-fpm 慢日志php慢日志需要在php-fpm.conf设置,如果使用源码包安装默认请执行下...

PHP Session ID的实现原理与实例

目录Session作用session 的工作机制:PHPSESSIONID的生产算法原理:php.ini配置如下:PHP Session工作原理PHPcli模式通过session_id()使用sess...