PHP调用拼多多API模板

拼多多的 API 模板就更简单了,前段时间刚放出来接口权限,上周开始对接完成。
都是无需授权的接口,所以也就不需要刷新 token 之类的事。
另外拼多多的 token 过期时间是 30 天,每次都要重新获取 accesstoken,麻烦。

/*拼多多API类*/
class PDDApi
{
    private $client_id = 'client_id';    //  你的client_id
    private $client_secret = 'client_secret';   //  你的client_secret
    private function GetPDDApi($apiType, $param)
    {
        $param['client_id'] = $this->client_id;
        $param['type'] = $apiType;
        $param['data_type'] = 'JSON';
        $param['timestamp'] = self::getMillisecond();
        ksort($param);    //  排序
        $str = '';      //  拼接的字符串
        foreach ($param as $k => $v) $str .= $k . $v;
        $sign = strtoupper(md5($this->client_secret. $str . $this->client_secret));    //  生成签名    MD5加密转大写
        $param['sign'] = $sign;
        $url = 'http://gw-api.pinduoduo.com/api/router';
        return self::curl_post($url, $param);
    }

    //  post请求
    private static function curl_post($url, $curlPost)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

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

你可能感兴趣的:(PHP)