近期项目需要,抖音团购的券码核销,网上找了好久也没找到,只能自己手敲了。。。话不多说,直接上代码吧! 凑合着用吧,反正能销,具体业务还得自己码代码了
近期项目需要,抖音团购的券码核销,网上找了好久也没找到,只能自己手敲了。。。话不多说,直接上代码吧! 凑合着用吧,反正能销,具体业务还得自己码代码了
PHPclass Test{ protected $Appid = ''; protected $AppSecret = ''; protected $pro_id = ''; // 门店id public function test(){ $encrypted_data = '100000000000000'; // code 格式 100000000000000 $url = 'https://open.douyin.com/Goodlife/v1/fulfilment/certificate/prepare/'; $client_token = $this->get_client_token(); if(!$client_token){ die('token 获取失败'); } $res = $this->prepareCertificate($client_token,'',$encrypted_data); $v = $res['data']['verify_token']; $code = $res['data']['certificates'][0]['encrypted_code']; $hexiao = $this->verifyCoupon($v,$client_token,$this->pro_id,[$code]); //核销之后的所有信息都在这里 打印自己看就行 print_r($hexiao); } public function prepareCertificate($access_token, $encrypted_data, $code = null) { $url = 'Https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/'; $params = []; if (!empty($encrypted_data)) { $params['encrypted_data'] = urlencode($encrypted_data); } elseif (!empty($code)) { $params['code'] = $code; } else { throw new Exception("encrypted_data 和 code 参数不能同时为空"); } $back = $this->k_request($url,$params,$access_token,0); return $back; } public function verifyCoupon($verifyToken,$client_token, $poiId, $encryptedCodes, $codes = null, $orderId = null, $codeWithTimeList = null, $voucher = null) { $url = 'https://open.douyin.com/goodlife/v1/fulfilment/certificate/verify/'; $data = array( 'verify_token' => $verifyToken, 'poi_id' => $poiId, 'encrypted_codes' => $encryptedCodes, 'codes' => $codes, ); $response = $this->k_request($url,$data,$client_token); return $response; } public function get_client_token(){ //正式环境:https://open.douyin.com/oauth/client_token/ //沙盒环境:https://open-sandbox.douyin.com/oauth/client_token/ $url = 'https://open.douyin.com/oauth/client_token/'; $param = [ 'client_key' => $this->Appid, 'client_secret' => $this->AppSecret, 'grant_type' => 'client_credential' ]; $res = $this->request($url,$param); $back = JSON_decode($res,true); if($back['data']['error_code'] == 0){ return $back['data']['access_token']; } } //curl 提交 public function request($url,$data=null,$https=true,$method='post'){ //1.初始化url $ch = curl_init($url); //2.设置相关的参数 //字符串不直接输出,进行一个变量的存储 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //判断是否为https请求 if($https === true){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } //判断是否为post请求 if($method == 'post'){ curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:multipart/fORM-data')); //3.发送请求 $str = curl_exec($ch); //4.关闭连接 curl_close($ch); //6.返回请求到的结果 return $str; } public function k_request($url,$data = [],$client_token = '',$is_post = 1){ $headers = [ 'Content-Type: application/json', 'access-token: ' . $client_token, ]; $curl = curl_init(); if($is_post){ curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENcoding => '', CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_HTTPHEADER => $headers, )); }else{ $data = http_build_query($data); curl_setopt_array($curl, [ CURLOPT_URL => "$url?$data", CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, ]); } $response = curl_exec($curl); curl_close($curl); return json_decode($response,true); }}
来源地址:https://blog.csdn.net/m0_47719288/article/details/130706360
--结束END--
本文标题: 抖音团购券码核销PHP
本文链接: https://www.lsjlt.com/news/400493.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0