PHP对接抖音开发平台接口的详细教程
- 学习教育
- 关注:2.35W次
一、说明
二、代码
三、代码运行需知
四、功能扩展
五、接口调用需要注意的点
六、接口文档中的 ‘坑'(以订单列表接口为例)
1、请求参数、响应参数 代表的具体值不清晰
一、说明
抖音开放平台-开发指南
二、代码
<?phpnamespace appcommonlibs;use appcommonexceptionBaseException;/** * Class DouYinApi * @package appcommonlibs */class DouYinApi{ private $host; //抖音接口API,API调用指南:https://op.jinritemai.com/docs/guide-docs/148/814 private $appKey; //appKey private $appSecret; //appSecret private $accessToken; //访问令牌 private $refreshToken; //刷新令牌 private $versionNumber; //API协议版本,当前版本为 2 private $versionNumberStr; //API协议版本,当前版本为 v2 public function __construct() { $this->host = 'https://openapi-fxg.jinritemai.com'; //接口访问地址 $this->appKey = '你的抖音后台的appKey'; $this->appSecret = '你的抖音后台的appSecret'; $this->versionNumber = '2'; $this->versionNumberStr = 'v' . $this->versionNumber; //获取access_token,refresh_token放到最后,如果其他的如versionNumber在后面设置则报错:"v不可为空",因为handleToken中调用了versionNumber,但versionNumber此时的值为NULL $result = self::handleToken(); //创建Token// $result = self::handleToken(false); //刷新Token:提示-"缺少code",需要建一张第三方表存抖音该店铺的access_token,refresh_token,expire_time信息 $this->accessToken = $result['access_token']; //用于出创建token接口之外的其他接口 $this->refreshToken = $result['refresh_token']; //用于刷新token接口 } /** * 处理(创建/刷新)Token的方法 * 开发指南 > 产品功能 > 授权介绍 -> 自用型应用店铺授权流程:https://op.jinritemai.com/docs/guide-docs/9/21 * @param bool $createToken 是否调用创建Token的方法 * @return array * @throws BaseException */ public function handleToken($createToken = true) { if ($createToken) { //调用创建token接口 $param = [ 'code' => '', 'grant_type' => 'authorization_self', 'shop_id' => '你抖音店铺的ID', //店铺ID,仅自用型应用有效;若不传,则默认返回最早授权成功店铺对应的token信息 ]; $method = 'token.create'; } else { //调用刷新Token方法 $param = [// 'app_id' => '', //应用key ,长度19位字母和数字组合的字符串,可不传 'refresh_token' => $this->refreshToken, //注意:传真实的refreshToken值,而不是传REFRESH_TOKEN 'grant_type' => 'refresh_token', ]; $method = 'token.refresh'; } $timestamp = time(); //接口请求前记录开始时间,防止过期时间$expireTime失效 $result = self::fetch($method, $param); if ($result['code'] != 10000) { //请求失败 throw new BaseException($result['message']); } else { $data = $result['data']; $accessToken = $data['access_token']; //accessToken $refreshToken = $data['refresh_token']; //refreshToken $expireTime = $timestamp + $data['expires_in']; //Token过期时间 = 当前时间 + 有效时间(秒s) return [ 'access_token' => $accessToken, 'refresh_token' => $refreshToken, ]; } } /** * 封装抖音接口公共方法 * PHP调用说明:https://op.jinritemai.com/docs/guide-docs/151/811 * @param $method 方法名:格式 token.create 方法中转为 token/create * @param $param 请求接口需要的参数名 * @param bool $accessToken url中是否要加上access_token,默认否。 * 为什么不直接传accessToken的值:在本类中,可以获取到accessToken的值,直接传,但是如果在其他的地方调用就获取不到access_token的值,需要传true/false标识在本类中获取。 * @param bool $paramJsonAddToUrl 是否把paramJson放到 url 中,根据实际情况 * 例:实际过程中【订单批量解密接口】不需要放到url中(猜测是这个接口paramJson内容太多,会超出GET的最大内容) * 订单批量解密接口:https://op.jinritemai.com/docs/api-docs/15/982 * @return false|mixed|string */ function fetch($method, $param, $accessToken = false, $paramJsonAddToUrl = true) { //当前时间戳 $timestamp = time(); //PHP中:如果数组为空转为json之后是[]。但接口可能是强类型语言编写的,需要传{}。所以$param为空时,需要把$paramJson设置为{} $paramJson = $param ? self::marshal($param) : '{}'; //获取签名 $sign = self::sign($method, $timestamp, $paramJson); //调用的方法.替换为/ $methodPath = str_replace('.', '/', $method); //拼接url路径 $url = $this->host . '/' . $methodPath . '?method=' . urlencode($method) . '&app_key=' . urlencode($this->appKey); if ($accessToken) { $url .= '&access_token=' .urlencode($this->accessToken); } $url .= '×tamp=' . urlencode(strval($timestamp)) . '&v=' . urlencode($this->versionNumber) . '&sign=' . $sign; if ($paramJsonAddToUrl) { $url .= '¶m_json=' . $paramJson; } $url .= '&sign_method=' . urlencode('hmac-sha256'); //官方接口为非必填,但是不加签名会验证失败 //处理句柄数据 $opts = array('http' => array( 'method' => 'POST', 'header' => "Accept: */*" . "Content-type: application/json;charset=UTF-8", 'content' => $paramJson ) ); $context = stream_context_create($opts); $result = file_get_contents($url, false, $context); return json_decode($result,true); } //计算签名 function sign($method, $timestamp, $paramJson) { $paramPattern = 'app_key' . $this->appKey . 'method' . $method . 'param_json' . $paramJson . 'timestamp' . $timestamp . $this->versionNumberStr; $signPattern = $this->appSecret . $paramPattern . $this->appSecret; return hash_hmac("sha256", $signPattern, $this->appSecret); } //序列化参数,入参必须为关联数组(键值对数组) function marshal(array $param) { self::rec_ksort($param); // 对关联数组中的kv,执行排序,需要递归 $s = json_encode($param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); // 重新序列化,确保所有key按字典序排序 // 加入flag,确保斜杠不被escape,汉字不被escape return $s; } //关联数组排序,递归 function rec_ksort(array &$arr) { $kstring = true; foreach ($arr as $k => &$v) { if (!is_string($k)) { $kstring = false; } if (is_array($v)) { self::rec_ksort($v); //这里的调用方式要和marshal中调用方式一致 } } if ($kstring) { ksort($arr); } }}
三、代码运行需知
在 __construct()
方法 $this->appKey
中加上你的真实
appKey
在 __construct()
方法 $this->appSecret
中加上你的真实
appSecret
在 handleToken()
方法 shop_id
中加上你真实的
抖音店铺ID
四、功能扩展
加一张数据表 third_shop(第三方店铺表)
:存放第三方店铺(比如:抖音)的信息,表的字段大致有:id;shop_name:店铺名;third_shop_id:第三方店铺的ID,source:店铺来源(抖音,京东,天猫);app_key,app_secret,access_token,refresh_token,expire_time:过期时间;status:状态(0-关闭;1-启用),create_time,update_time ...
我们要对接抖音前,在third_shop
中写好 id;shop_name:店铺名;third_shop_id:第三方店铺的ID,source:店铺来源(抖音,京东,天猫);app_key,app_secret;status:状态(0-关闭;1-启用),create_time,update_time ....
在 __construct()
中先查询店铺的信息,如果 access_token为空
或者 expire_time过期时间 小于 当前时间
,则需要重新生成 access_token,refresh_token,expire_time:过期时间
在 handleToken()
中加上third_shop 表
更新操作;否则取数据表中未过期的 access_token,refresh_token
用于接口调用
五、接口调用需要注意的点
1、param为空的问题:param为空,$paramJson字符串的值为 {}
,而不是 []
2、rec_ksort递归调用的问题:rec_ksort中调用rec_ksort方式要和marshal中调用rec_ksort方式一致
3、paramJson何时传的问题:如果接口请求数据太大,GET请求可能会超出最大值,则 fetch()
中 $paramJsonAddToUrl
可试着传 false
六、接口文档中的 ‘坑'(以订单列表接口为例)
1、请求参数、响应参数 代表的具体值不清晰
订单列表中请求参数、响应参数main_status,每个数字代表什么意思,没有清楚的给出,如下图:
给了,在订单详情 接口的 响应参数
中,如下图:
2、页码从第0页开始(这个属于需要注意的点)
3、金额 是元 还是 分,不清晰
不给的话,那就默认为:分
到此这篇关于PHP对接抖音开发平台接口的详细教程的文章就介绍到这了,希望大家以后多多支持好二三四!
<link rel="stylesheet" href="https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/260993986ac4/2615a99b7dd2.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/260993986ac4/2615be9c6ada531262c882c854df.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a29d68df5a3f60c697d84a.js"></script> <script>SyntaxHighlighter.autoloader( 'applescript https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1777de8fd86bc8459da050.js', 'actionscript3 as3 https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e17549d.js', 'bash shell https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1466dd8b.js', 'coldfusion cf https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1568c287fb4dd85e9bbe.js', 'cpp c https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1577de.js', 'obj-c objc https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1965c4a0.js', 'c# c-sharp csharp https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1554c682cf48.js', 'css https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1574dd.js', 'delphi pascal https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1262c293d551.js', 'diff patch pas https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e126ec885.js', 'erl erlang https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1375c282d35f.js', 'groovy https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1175c18ccb41.js', 'haxe hx https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1e66d686.js', 'java https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1c66d882.js', 'jfx javafx https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1c66d882fb60.js', 'js jscript javascript https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1c54cd91d448df.js', 'perl pl https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e0662dc8f.js', 'php https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e066fde.js', 'text plain https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e066bcf8ad3.js', 'py python https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e067eda8bd256.js', 'ruby rails ror rb https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e0472cc9a.js', 'scala https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e0564cf8fdc.js', 'sql https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e0576c2.js', 'vb vbnet https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e0065.js', 'ps powershell https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e0668d986cf6bc35298bc.js', 'xml xhtml xslt html https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e0e6ac2.js', 'go golang https://js.how234.com/3f0e869d6d/260484806ecf5e3f60c68fd45fc34391a2/261e989d7fc34564379cd1/2615a8867ac45e1168.js' );</script> <script type="text/javascript"> SyntaxHighlighter.all(); </script>
- 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/xuexijiaoyu/5yo5d0.html