zoukankan      html  css  js  c++  java
  • 京东联盟API简易PHP接口类

    https://union.jd.com/helpcenter/13246-13247-46301

    官方默认只有java的sdk

    <?php
    /**
     * Created by PhpStorm.
     * User: Leestar
     * Mail: lixin9610@126.com
     */
    
    namespace appcommon;
    
    use thinkfacadeLog;
    
    /**
     * 京东联盟
     * Class JdUnion
     * @package appcommon
     */
    class JdUnion
    {
        const APP_KEY = '主体appkey';
        const SECRET_KEY = '主体secret';
        const URL = 'https://router.jd.com/api';
    
        /**
         * 根据关键字返回商品列表
         * https://union.jd.com/openplatform/debug/10420
         * @param array $params 见文档的业务入参
         * @return bool|array
         */
        public static function goodsQuery($params)
        {
            $res = self::query('jd.union.open.goods.query', [
                'goodsReqDTO' => $params,
            ]);
            if (isset($res['jd_union_open_goods_query_response']) && $res['jd_union_open_goods_query_response']['code'] == '0') {
                $temp = json_decode($res['jd_union_open_goods_query_response']['result'], true);
                if ($temp['code'] == 200) {
                    return $temp;
                }
            }
            Log::write($res);
            return false;
        }
    
        /**
         * 社交媒体获取推广链接接口【申请】
         * https://union.jd.com/openplatform/api/10424
         * @param int $skuid 商品id
         * @return bool|array
         */
        public static function promotionUrl($skuid)
        {
            $res = self::query('jd.union.open.promotion.bysubunionid.get', [
                'promotionCodeReq' => [
                    'materialId' => 'https://wqitem.jd.com/item/view?sku=' . $skuid,
                    'positionId' => 推广位id
                ],
            ]);
            if (isset($res['jd_union_open_promotion_bysubunionid_get_response']) && $res['jd_union_open_promotion_bysubunionid_get_response']['code'] == '0') {
                return json_decode($res['jd_union_open_promotion_bysubunionid_get_response']['result'], true);
            } else {
                Log::write($res);
                return false;
            }
        }
    
        /**
         * 京粉精选商品查询接口
         * https://union.jd.com/openplatform/api/10417
         * @param array $params 见文档的业务入参
         * @return bool|array
         */
        public static function jingFenQuery($params)
        {
            $res = self::query('jd.union.open.goods.jingfen.query', [
                'goodsReq' => $params,
            ]);
            if (isset($res['jd_union_open_goods_jingfen_query_response']) && $res['jd_union_open_goods_jingfen_query_response']['code'] == '0') {
                $temp = json_decode($res['jd_union_open_goods_jingfen_query_response']['result'], true);
                if ($temp['code'] == 200) {
                    return $temp;
                }
            }
            Log::write($res);
            return false;
        }
    
        /**
         * 基础查询方法
         * @param string $method 方法名
         * @param array $business_data 业务入参
         * @return array
         */
        private static function query($method, $business_data)
        {
            //业务参数
            $param_json = json_encode($business_data);
            //系统参数
            $system_data = [
                'v'            => '1.0',
                'method'       => $method,
                'access_token' => '',
                'app_key'      => self::APP_KEY,
                'sign_method'  => 'md5',
                'format'       => 'json',
                'timestamp'    => date("Y-m-d H:i:s"),
                'sign'         => '',
                'param_json'   => $param_json
            ];
            //签名
            $orderData = self::paramOrder($system_data);
            $sign = self::paramSign($orderData);
            $system_data['sign'] = $sign;
            return json_decode(self::post($system_data), true);
        }
    
        //排序ksort
        private static function paramOrder($params)
        {
            ksort($params);
            $stringToBeSigned = "";
            $i = 0;
            foreach ($params as $k => $v) {
                if (false === self::checkEmpty($v)) {
                    $v = self::characet($v, 'utf-8');
                    if ($i == 0) {
                        $stringToBeSigned .= "$k" . "$v";
                    } else {
                        $stringToBeSigned .= "$k" . "$v";
                    }
                    $i++;
                }
            }
            unset ($k, $v);
            return $stringToBeSigned;
        }
    
        //为空检查
        private static function checkEmpty($value)
        {
            if (!isset($value))
                return true;
            if ($value === null)
                return true;
            if (trim($value) === "")
                return true;
            return false;
        }
    
        //编码设置
        private static function characet($data, $targetCharset)
        {
            if (!empty($data)) {
                $fileType = 'utf-8';
                if (strcasecmp($fileType, $targetCharset) != 0) {
                    $data = mb_convert_encoding($data, $targetCharset, $fileType);
                }
            }
            return $data;
        }
    
        //签名
        private static function paramSign($params)
        {
            $str = self::SECRET_KEY . $params . self::SECRET_KEY;
            $sign = md5($str);
            return strtoupper($sign);
        }
    
        //基于curl提交
        private static function post($param)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, self::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;
        }
    }

    参考:

    https://blog.csdn.net/u013252962/article/details/100972707

     
  • 相关阅读:
    算法与数据结构(1):基础部分——以插入排序为例
    软件工程结对作业
    软件工程第1次作业
    软件工程第0次作业
    python爬虫随笔(2)—启动爬虫与xpath
    python爬虫随笔-scrapy框架(1)——scrapy框架的安装和结构介绍
    【面试题】String类、包装类的不可变性
    【面试题】Java类初始化和实例初始化的顺序
    【面试题】Java单例设计模式-饿汉式枚举(enum)单例
    【面试题】从JVM的角度去理解i++和++i
  • 原文地址:https://www.cnblogs.com/leestar54/p/13204019.html
Copyright © 2011-2022 走看看