• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

PHP对接美团API接口 实现卡卷核销功能

武飞扬头像
不耍PHP了,搞C
帮助1

下面的只是一些PHP代码,与其相对应的还有一个pdf文档【里面叙述了都需要那些必要条件,还有运行演示】,大家可以去GitHub上去下载。

链接:GitHub - ITLYS/Warehouse: 一个小白的成长历程

  1.  
    <?php
  2.  
    namespace Admin\Controller\NestingHTML;
  3.  
    use Admin\Controller\WeiXinController;
  4.  
    use Think\Controller;
  5.  
    class IndexController extends WeiXinController{
  6.  
     
  7.  
     
  8.  
    /* @FunctionDesc:美团卡卷 查询、验券(核销),撤销验券
  9.  
    * @Params: qr_code 卡券券码
  10.  
    * open_shop_uuid 店铺id (根据session值 获取得到)
  11.  
    * deal_id 套餐id 查询接口获得
  12.  
    * type 功能类型 值为 save 验券(核销)、值为 cancel 验券(核销)、其他值时为查询
  13.  
    * */
  14.  
    public function tuangou_hexiao(){
  15.  
    //
  16.  
    $qr_cpde = $_GET['qr_code'];
  17.  
    $open_shop_uuid = $_GET['open_shop_uuid'];
  18.  
     
  19.  
    // $arr 为数组 里面为应用参数
  20.  
    $appKey = "111111111";
  21.  
    $secret = "11111111111111111"; //秘钥
  22.  
    $timestamp = date('Y-m-d H:i:s');
  23.  
    $format = 'json';
  24.  
    $v = 1;
  25.  
    $sign_method = 'MD5';
  26.  
    //因为功能要实现在小程序,所以获取的必要信息要存在文件中,有必要时拿出来
  27.  
    //不是必要行为,
  28.  
    $file = $this->readFile();
  29.  
     
  30.  
    $data = [
  31.  
    'app_key' => $appKey,
  32.  
    'timestamp' => $timestamp,
  33.  
    'sign_method' => $sign_method,
  34.  
    'format' => $format,
  35.  
    'v' => $v,
  36.  
    //此处的session值,我原本是存在文件中的,
  37.  
    //如果你不需要存文件,那你就当做参数传递过来
  38.  
    'session' => $file['session'],
  39.  
    ];
  40.  
    //根据不同的操作类型配置不同的参数
  41.  
    //具体参数含义见 https://open.dianping.com/document/v2?docId=6000176&rootDocId=5000
  42.  
    if($_GET['type']=='save'){
  43.  
    $arr = [
  44.  
    'requestid' => '123',
  45.  
    'count'=>1,
  46.  
    'receipt_code' => $qr_cpde,
  47.  
    'open_shop_uuid' => $open_shop_uuid,
  48.  
    'app_shop_account' => '账号',
  49.  
    'app_shop_accountname' => '账号名称',
  50.  
    ];
  51.  
    $url = 'https://openapi.dianping.com/router/tuangou/receipt/consume';//验券(核销)
  52.  
    }elseif ($_GET['type']=='cancel'){
  53.  
    $arr = [
  54.  
    'app_deal_id' => $_GET['deal_id'],
  55.  
    'receipt_code' => $qr_cpde,
  56.  
    'open_shop_uuid' => $open_shop_uuid,
  57.  
    'app_shop_account' => '账号',
  58.  
    'app_shop_accountname' => '账号名称',
  59.  
    ];
  60.  
    $url = 'https://openapi.dianping.com/router/tuangou/receipt/reverseconsume';//撤销
  61.  
    }else{
  62.  
    $arr = [
  63.  
    'receipt_code' => $qr_cpde,
  64.  
    'open_shop_uuid' => $open_shop_uuid,
  65.  
    ];
  66.  
    $url = 'https://openapi.dianping.com/router/tuangou/receipt/prepare';//查询券
  67.  
    }
  68.  
     
  69.  
    $data = array_merge($data, $arr);
  70.  
    ksort($data);
  71.  
    $sign = $this->call_sign($secret, $data);//获取签名
  72.  
    $data['sign'] = $sign;
  73.  
    $data = array_merge($data, $arr);
  74.  
    $postdata = http_build_query($data);
  75.  
    $tmpInfo= $this->curl_post($url,$postdata);
  76.  
    $this->ReturnSucess($tmpInfo);
  77.  
     
  78.  
    }
  79.  
     
  80.  
    /* @FunctionDesc:商家授权
  81.  
    * @Params: auth_code 用户同意授权后,回调得到的值,根据该值可获取session(也就是auth_token)
  82.  
    * */
  83.  
    public function get_auth(){
  84.  
    $auth_code = $_GET["auth_code"];
  85.  
    //判断是否为回调
  86.  
    if(empty($auth_code)) {
  87.  
    $app_key='11111111111';
  88.  
    $state='teststate';
  89.  
    //回调的url,我配置为当前方法。
  90.  
    $redirect_url='https:你的路径/index/get_auth';
  91.  
    $scope='tuangou';
  92.  
    $url='https://e.dianping.com/dz-open/merchant/auth?';
  93.  
    $data=[
  94.  
    'app_key' =>$app_key,
  95.  
    'state' => $state,
  96.  
    'redirect_url'=>$redirect_url
  97.  
    ];
  98.  
    $postdata = http_build_query($data);
  99.  
    header("Location: $url$postdata");
  100.  
    } else {
  101.  
    trace('回调成功'.$auth_code);
  102.  
    //根据auth_code 获取session的授权码
  103.  
    $tmpInfo = $this->get_session($auth_code);
  104.  
    //根据$session $bid 获取店铺id
  105.  
    $shopInfo = $this->get_shopid($tmpInfo['access_token'],$tmpInfo['bid']);
  106.  
    }
  107.  
    }
  108.  
     
  109.  
     
  110.  
    //授权获取session
  111.  
    public function get_session($auth_code){
  112.  
    $app_key='1111111';
  113.  
    $app_secret='111111111111111111111';
  114.  
    $grant_type='authorization_code';
  115.  
    $redirect_url='https:你的路径/index/get_auth';
  116.  
    $data=[
  117.  
    'app_key' =>$app_key,
  118.  
    'app_secret' => $app_secret,
  119.  
    'redirect_url' =>$redirect_url,
  120.  
    'auth_code' =>$auth_code,
  121.  
    'grant_type' =>$grant_type
  122.  
    ];
  123.  
    $postdata = http_build_query($data);
  124.  
    $url='https://openapi.dianping.com/router/oauth/token';
  125.  
    $tmpInfo=$this->curl_post($url,$postdata);
  126.  
    return $tmpInfo;
  127.  
    }
  128.  
     
  129.  
     
  130.  
    //获取所有店铺的id
  131.  
    public function get_shopid($session,$bid){
  132.  
    $app_key='11111111';
  133.  
    $secret = "111111111111111111111111"; //秘钥
  134.  
    $sign_method='MD5';
  135.  
    $timestamp = date('Y-m-d H:i:s');
  136.  
    $format = 'json';
  137.  
    $v = 1;
  138.  
    $offset =0;
  139.  
    $limit = 20;
  140.  
    $url='https://openapi.dianping.com/router/oauth/session/scope?';
  141.  
    $data=[
  142.  
    'app_key' =>$app_key,
  143.  
    'sign_method' => $sign_method,
  144.  
    'timestamp' =>$timestamp,
  145.  
    'format' =>$format,
  146.  
    'v' =>$v,
  147.  
    'session' =>$session,
  148.  
    'bid' =>$bid,
  149.  
    'offset' =>$offset,
  150.  
    'limit' =>$limit,
  151.  
    ];
  152.  
    ksort($data);
  153.  
    $sign = $this->call_sign($secret, $data);
  154.  
    $data['sign'] = $sign;
  155.  
    $postdata = http_build_query($data);
  156.  
    $tmpInfo=$this->curl_get($url.$postdata);
  157.  
    return $tmpInfo;
  158.  
    }
  159.  
    /*
  160.  
    * 写入文件
  161.  
    * */
  162.  
    public function createFile($data){
  163.  
    //创建一个文件
  164.  
    $file = fopen('MeiTuan.txt','w');
  165.  
     
  166.  
    //写入文件
  167.  
    fwrite($file, json_encode($data));
  168.  
     
  169.  
    //关闭文件
  170.  
    fclose($file);
  171.  
    }
  172.  
    /*
  173.  
    * 读取文件
  174.  
    * */
  175.  
    public function readFile(){
  176.  
    $file = fopen('MeiTuan.txt','r');
  177.  
     
  178.  
    $data = fread( $file , filesize('MeiTuan.txt') );
  179.  
     
  180.  
    fclose($file);
  181.  
     
  182.  
    return json_decode( $data , true );
  183.  
    }
  184.  
     
  185.  
    /**
  186.  
    * 计算签名
  187.  
    *
  188.  
    * @param $app_secret 三方app_secret
  189.  
    * @param $req_param 请求参数集合,包括公共参数和业务参数
  190.  
    * @return string md5签名
  191.  
    */
  192.  
    function call_sign($secret, $data)
  193.  
    {
  194.  
    // 排序所有请求参数
  195.  
    ksort($data);
  196.  
    $src_value = "";
  197.  
    // 按照key1value1key2value2...keynvaluen拼接
  198.  
    foreach ($data as $key => $value) {
  199.  
    $src_value .= ($key . $value);
  200.  
    }
  201.  
    //计算md5
  202.  
    return md5($secret . $src_value . $secret);
  203.  
    }
  204.  
     
  205.  
    //post请求
  206.  
    function curl_post($url,$postdata){
  207.  
    $curl = curl_init(); // 启动一个CURL会话
  208.  
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  209.  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
  210.  
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  211.  
    'Expect:'
  212.  
    )); // 解决数据包大不能提交
  213.  
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  214.  
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  215.  
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  216.  
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); // Post提交的数据包
  217.  
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
  218.  
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  219.  
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
  220.  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  221.  
    // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  222.  
    $tmpInfo = curl_exec($curl); // 执行操作
  223.  
    if (curl_errno($curl)) {
  224.  
    echo 'Errno' . curl_error($curl);
  225.  
    }
  226.  
    curl_close($curl); // 关键CURL会话
  227.  
    $tmpInfo=json_decode($tmpInfo,true);
  228.  
    return $tmpInfo;
  229.  
    }
  230.  
     
  231.  
    //get请求
  232.  
    private function curl_get($url) {
  233.  
    //初使化curl
  234.  
    $curl = curl_init();
  235.  
    //请求的url,由形参传入
  236.  
    curl_setopt($curl, CURLOPT_URL, $url);
  237.  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
  238.  
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  239.  
    'Expect:'
  240.  
    )); // 解决数据包大不能提交
  241.  
    //将得到的数据返回
  242.  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  243.  
    //不处理头信息
  244.  
    curl_setopt($curl, CURLOPT_HEADER, 0);
  245.  
    //连接超过10秒超时
  246.  
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  247.  
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  248.  
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  249.  
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  250.  
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  251.  
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
  252.  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  253.  
    //执行curl
  254.  
    $output = curl_exec($curl);
  255.  
    if (curl_errno($curl)) {
  256.  
    echo 'Errno' . curl_error($curl);
  257.  
    }
  258.  
    //关闭资源
  259.  
    curl_close($curl);
  260.  
    //返回内容
  261.  
    $tmpInfo=json_decode($output,true);
  262.  
    return $tmpInfo;
  263.  
    }
  264.  
    }
  265.  
     
  266.  
     
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaejib
系列文章
更多 icon
同类精品
更多 icon
继续加载