PHP 调用 拼多多 API 模板

in PHP with 0 comment

拼多多的API模板就更简单了,前段时间刚放出来接口权限,上周开始对接完成。
都是无需授权的接口,所以也就不需要刷新token之类的事。

首先按照文档申请接入多多客(打开后选择左侧多多客接入指南):http://open.pinduoduo.com/#/document?auth=1

<?php
/*拼多多API类*/
class PDDApi
{
    private $client_id = 'client_id';    //  你的client_id
    private $client_secret = 'client_secret';   //  你的client_secret

    /**
     * 获取拼多多接口数据
     * @param string $apiType  API 名称 如:pdd.ddk.direct.goods.query
     * @param array $param  公共参数 如:['page' => 1 , 'page_size' => 100]
     * @return mixed
     */
    public function GetPDDApi($apiType, $param)
    {
        $url = 'http://gw-api.pinduoduo.com/api/router';
        $param['client_id'] = $this->client_id;
        $param['type'] = $apiType;
        $param['data_type'] = 'JSON';
        $param['timestamp'] = $this->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;
        return $this->curl_post($url, $param);
    }

    /**
     * 发送post请求
     * @param $url
     * @param $curlPost
     * @return mixed
     */
    private 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 function getMillisecond()
    {
        list($t1, $t2) = explode(' ', microtime());
        return sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
    }
}
//  调用方法
$p = new PDDApi();
echo $p->GetPDDApi('pdd.ddk.direct.goods.query',['page'=>1]);
Responses