淘寶客api接入步驟詳解

淘寶聯盟開放平臺:https://open.alimama.com/#!/document

 

一、創建應用

淘寶聯盟生態夥伴:https://www.alimama.com/member/login.htm?forward=http%3A%2F%2Fpub.alimama.com%2Fthird%2Fmanage%2Frecord%2Fstep.htm%3Fcategorie%3D0%26step%3D1

推廣管理>添加備案 

審覈成功後點擊appkey申請,跳轉到阿里開放平臺應用管理

查看appkey appsecret

下載sdk,注意,sdk中只有擁有api權限的文件

設置回調url:

api權限申請:一是淘寶聯盟開放平臺功能中心,一是阿里開放平臺

     阿里開放平臺申請權限,一般填寫理由後就能通過

淘寶客api文檔:

sessionkey刷新,第一次需手動獲取,授權介紹:https://open.taobao.com/doc.htm?docId=105&docType=1#s2

/**
     * 獲取 更新sessionkey
     * 
     * @return mixed
     */
    public function refreshSessionKey()
    {
        $sessionKeyData = file_get_contents(app_path('Lib\Alimm\sessionkey.json'));

        if (empty($sessionKeyData)) {

        } else {
            $sessionKeyData = json_decode($sessionKeyData, true);

            //sessionkey是否過期過期前一天更新session
            if ($sessionKeyData['expires_in'] * 1000 + $sessionKeyData['ts'] < self::getMillisecond()){
                $sign = $this->encodeHexString($this->md5Hex(getSortParams([
                        'appkey' => $this->appkey,
                        'refresh_token' => $sessionKeyData['refresh_token'],
                        'sessionkey' => $sessionKeyData['top_session'],
                    ]).$this->secretKey));

                $url = "http://container.open.taobao.com/container/refresh?appkey=$this->appkey&refresh_token={$sessionKeyData['refresh_token']}&sessionkey={$sessionKeyData['top_session']}&sign=".strtoupper($sign);

                $result = file_get_contents($url);

                $result = json_decode($result, true);

                $result['ts'] = self::getMillisecond();

                file_put_contents(app_path('Lib\Alimm\sessionkey.json'), json_encode($result));

                return $result['top_session'];

            } else {

                return $sessionKeyData['top_session'];
            }
        }

    }

    /**
     * 16進制轉string拼接
     * @author Lerko
     * @dateTime 2018-01-25T10:18:31+0800
     * @param    array                    $bytes [description]
     * @return   [type]                          [description]
     */
    public function encodeHexString(array $bytes){
        $LOWER=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
        $length=count($bytes);
        $charArr=[];
        foreach ($bytes as $value) {
            $value=intval($value);
            $charArr[]=$LOWER[ $this->uright(0xF0 & $value,4) ];
            $charArr[]=$LOWER[ 0x0F & $value ];
        }
        return implode("",$charArr);
    }

    /** php 無符號右移 */
    public function uright($a, $n)
    {
        $c = 2147483647>>($n-1);
        return $c&($a>>$n);
    }

    /**
     * 模擬DigestUtils.md5
     * @author Lerko
     * @dateTime 2018-01-25T09:28:33+0800
     * @param    [string]                   $string 加密字符
     * @return   [array]                           加密之後的byte數組
     */
    public function md5Hex($string)
    {
        return unpack("c*", md5($string,true));
    }

    /**
     * 拼接簽名字符串
     *
     * @param array $param
     * @return string
     */
    public function getSortParams($param = [])
    {
        unset($param['sign']);
        ksort($param);
        $signstr = '';
        if (is_array($param)) {
            foreach ($param as $key => $value) {
                if ($value == '') {
                    continue;
                }
                $signstr .= $key .$value;
            }
        }
        return $signstr;
    }

    /**
     * 獲取13位時間戳
     *
     * @return string
     */
    private static function getMillisecond()
    {
        list($t1, $t2) = explode(' ', microtime());
        return sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
    }

首次獲取:http://container.api.taobao.com/container?appkey={appkey}

回調:

public function callback()
{
        $top_appkey = $_GET['top_appkey'];
        $top_parameters = $_GET['top_parameters'];
        $top_session = $_GET['top_session'];
        $top_sign = $_GET['top_sign'];

        $secret = '61dd8bb9c0b78b175faad8b077c232e3'; // 別忘了改成你自己的

        $md5 = md5( $top_appkey . $top_parameters . $top_session . $secret, true );
        $sign = base64_encode( $md5 );

        if ( $sign != $top_sign ) {
            echo "signature invalid.";
            exit();
        }

        $parameters = array();
        parse_str(base64_decode( $top_parameters ), $parameters );

        $parameters['top_session'] = $top_session;

        file_put_contents(app_path('Lib\Alimm\sessionkey.json'), json_encode($parameters));
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章