京东联盟API简易PHP接口类

2021-01-03 12:28

阅读:642

标签:iss   curl   参数   tar   getch   time   业务   date   help   

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

官方默认只有java的sdk

php
/**
 * Created by PhpStorm.
 * User: Leestar
 * Mail: lixin9610@126.com
 */

namespace app\common;

use think\facade\Log;

/**
 * 京东联盟
 * Class JdUnion
 * @package app\common
 */
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

 

京东联盟API简易PHP接口类

标签:iss   curl   参数   tar   getch   time   业务   date   help   

原文地址:https://www.cnblogs.com/leestar54/p/13204019.html


评论


亲,登录后才可以留言!