记拼多多快团团api php 调用接口类

<?php
namespace app\common;
class Pdd{
 
    public  $client_id = ''; // 你的client_id
    public  $client_secret = ''; // 你的client_secret
    public  $url = 'http://gw-api.pinduoduo.com/api/router';

    //授权地址
    public function getAPIClient($redirectUrl,$state='')
    {
        $url="https://oauth.pinduoduo.com/authorize/ktt?client_id={$this->client_id}&redirect_uri={$redirectUrl}&state={$state}";
//         https://oauth.pinduoduo.com/authorize/ktt?client_id={此处拼接应用信息对应的client_id}&redirect_uri={此处拼接您创建应用时设置的回调地址redirect_uri}&state={自定义值,传入与返回一致,用于自定义当前授权特征并进行维护}
        return $url;
    }
    

    /**
     * post请求  请求ACCESS_TOKEN
     * Enter description here ...
     * @param unknown_type $url
     * @param unknown_type $curlPost
     */
     
    
    public  function curl_post_json($url, $data)
    {
        foreach ($data as $k=>$v) {
            if(is_array($v)){
                $data[$k] = json_encode($v);
            }
        }
        $json = json_encode($data);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置超时
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
        $rtn = curl_exec($ch);
        curl_close($ch);
        return json_decode($rtn,true);
        return $rtn;
    }
 
    public  function GetPDDApi($apiType, $param)
    {
        $param['client_id'] = $this->client_id;
        $param['type'] = $apiType;
        $param['data_type'] = 'JSON';
        $param['timestamp'] = self::getMillisecond();

        $str  =  $this->signStr($param);
        $sign = strtoupper(md5($this->client_secret. $str . $this->client_secret)); // 生成签名 MD5加密转大写
        $param['sign'] = $sign;
        return  $data=self::curl_post_json($this->url, $param);
        
    }    
    
    private function signStr($arr){
        $str = '';
        ksort($arr); // 排序
        foreach($arr as $k=>$v){
            if(is_array($v)){
               $v = json_encode($v);
            }
            $str .= $k . $v;
        }
        return $str;
    }

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

    /**
     * post请求
     * Enter description here ...
     * @param unknown_type $url
     * @param unknown_type $curlPost
     */
    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);
                // curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result,true);
        return $result;
    }
 
}

 

posted @ 2022-01-05 11:22  Liiu  阅读(980)  评论(1编辑  收藏  举报