網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > 學習教育 > 

PHP對接抖音開發平臺接口的詳細教程

欄目: 學習教育 / 發佈於: / 人氣:2.15W

一、說明

二、代碼

三、代碼執行需知

四、功能擴展

五、接口調用需要注意的點

六、接口文檔中的 ‘坑'(以訂單列表接口爲例)

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 .= '&timestamp=' . urlencode(strval($timestamp)) .            '&v=' . urlencode($this->versionNumber) .            '&sign=' . $sign;        if ($paramJsonAddToUrl) {            $url .= '&param_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,每個數字代表什麼意思,沒有清楚的給出,如下圖:

PHP對接抖音開發平臺接口的詳細教程

給了,在訂單詳情 接口的 響應參數 中,如下圖:

PHP對接抖音開發平臺接口的詳細教程 第2張

2、頁碼從第0頁開始(這個屬於需要注意的點)

PHP對接抖音開發平臺接口的詳細教程 第3張

3、金額 是元 還是 分,不清晰

不給的話,那就默認爲:分

PHP對接抖音開發平臺接口的詳細教程 第4張

到此這篇關於PHP對接抖音開發平臺接口的詳細教程的文章就介紹到這了,希望大家以後多多支援好二三四!

<link rel="stylesheet" href="https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da5ee921b51c/da42d322a20a.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da5ee921b51c/da42c425b502dd685f0fcdad1b74.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d824b707d4455d01d8bd05.js"></script> <script>SyntaxHighlighter.autoloader( 'applescript https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06d4a19c0bd24631b48642f.js', 'actionscript3 as3 https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06d695a.js', 'bash shell https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06e5b1ac4.js', 'coldfusion cf https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06f5505c89e0273004e7a.js', 'cpp c https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06f4a19.js', 'obj-c objc https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0635803ef.js', 'c# c-sharp csharp https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06f6901cdaa07.js', 'css https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06f491a.js', 'delphi pascal https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0685f05dcb01e.js', 'diff patch pas https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd068530fca.js', 'erl erlang https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0694805cdb610.js', 'groovy https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06b4806c3ae0e.js', 'haxe hx https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0645b11c9.js', 'java https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0665b1fcd.js', 'jfx javafx https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0665b1fcd9e2f.js', 'js jscript javascript https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd066690adeb10774.js', 'perl pl https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c5f1bc0.js', 'php https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c5219.js', 'text plain https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c5608c5b6.js', 'py python https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c431dc4b719.js', 'ruby rails ror rb https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07e4f0bd5.js', 'scala https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07f5908c0b9.js', 'sql https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07f4b05.js', 'vb vbnet https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07a58.js', 'ps powershell https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c551ec9aa24680c4d78.js', 'xml xhtml xslt html https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0745705.js', 'go golang https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06b55.js' );</script> <script type="text/javascript"> SyntaxHighlighter.all(); </script>