PHP 對接美團大眾點評團購券(門票)的開發步驟

一、功能簡要介紹

    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_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、授權地址:

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美團大眾點評團購券內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found