Jingdong league source PHP Interface

Recently added APP Jingdong Mall Union, the following is the interface code

<?php
namespace app\jingdong\controller;
use cmf\controller\HomeBaseController;

class IndexController extends HomeBaseController
{
    protected $appkey = 'your appkey';
    protected $secretkey= 'your secretkey';
    public $url = 'https://router.jd.com/api';

    /***
     * @return json
     * 京粉精选商品查询接口
     */
    public function jdList(){
        $eliteId = input('eliteId');
        $page  = input('page')?input('page'):1;
        $pagesize = input('pagesize')?input('pagesize'):20;
        $order = input('order')?input('order'):'inOrderCount30DaysSku';
        //系统参数
        $business_data = array(
            'goodsReq' => array(
                'eliteId' => $eliteId,
                'pageIndex' => $page,
                'pageSize' => $pagesize,
                'sortName' => $order,
                'sort' => 'desc'
            )
        );
        $param_json = json_encode($business_data);
        $system_data = array(
            'method' => 'jd.union.open.goods.jingfen.query',
            'app_key' => $this->appkey,
            'timestamp' => date("Y-m-d H:i:s"),
            'format' => 'json',
            'v' => '1.0',
            'sign_method' => 'md5',
            'sign' => '',
            'param_json' => $param_json
        );

        $orderData = $this->paramOrder($system_data);
        $sign = $this->paramSign($orderData);
        $system_data['sign'] = $sign;
        $res = $this->apiSign($system_data);
        $arr =  json_decode($res,true);
        $res = $arr['jd_union_open_goods_jingfen_query_response']['result'];
        return $res;
    }

    /***
     * 获取推广商品信息接口
     */
    public function jdDetail(){
        $skuId = input('skuId');
        $business_data = array(
            'skuIds' => $skuId
        );
        $param_json = json_encode($business_data);
        $system_data = array(
            'method' => 'jd.union.open.goods.promotiongoodsinfo.query',
            'app_key' => $this->appkey,
            'timestamp' => date("Y-m-d H:i:s"),
            'format' => 'json',
            'v' => '1.0',
            'sign_method' => 'md5',
            'sign' => '',
            'param_json' => $param_json
        );
        $orderData = $this->paramOrder($system_data);
        $sign = $this->paramSign($orderData);
        $system_data['sign'] = $sign;
        $res = $this->apiSign($system_data);
        $arr =  json_decode($res,true);
        $res = $arr['jd_union_open_goods_promotiongoodsinfo_query_response']['result'];
        return $res;
    }
	
	//排序ksort
    public function paramOrder($params){
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (false === $this->checkEmpty($v)) {
                $v = $this->characet($v, 'utf-8');
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "$v";
                } else {
                    $stringToBeSigned .=  "$k" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }
	//为空检查
    protected function checkEmpty($value) {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;
        return false;
    }
	//编码设置
    public function characet($data, $targetCharset) {
        if (!empty($data)) {
            $fileType = 'utf-8';
            if (strcasecmp($fileType, $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset, $fileType);
            }
        }
        return $data;
    }
	//签名
    public function paramSign($params){
        $str = $this->secretkey.$params.$this->secretkey;
        $sign = md5($str);
        return strtoupper($sign);
    }

    public function apiSign($param){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}

Here Insert Picture Description

Published 62 original articles · won praise 11 · views 8064

Guess you like

Origin blog.csdn.net/u013252962/article/details/100972707
Recommended