京东联盟新版API接口PHP版SDK的坑

今天测试了下京东联盟的API,新版看起来和淘宝很像了,操作起来应该很容易!下载SDK,SDK已经封装了好了,就不用自己去封装拼接!

京东联盟新版API接口PHP版SDK的坑_第1张图片

 看了一下,有composer.json文件,想着可能需要composer install一下,最后出错了,这一点比较喜欢拼多多的API接口,里面直接就有调用demo。composer install之后可以配置下APIKEY就可以直接访问!

出错了就自己来,进入jd,接口和阿里妈妈的API接口结构很像

京东联盟新版API接口PHP版SDK的坑_第2张图片

合并下JdClient.php和RequestCheckUtil.php(习惯尽量将文件合并,以免有太多的include)测试下jd.union.open.promotion.common.get接口,查看下文档示例

$c = new JdClient();
$c->appKey = appKey;
$c->appSecret = appSecret;
$c->accessToken = accessToken;
$c->serverUrl = SERVER_URL;
$req = new UnionOpenPromotionCommonGetRequest();
$promotionCodeReq= new PromotionCodeReq;
$req->setPromotionCodeReq($promotionCodeReq);
$req->setVersion("1.0");
$resp = $c->execute($req, $c->accessToken);

 修改下,像token不需要,version不需要就删除!调整完是

include "______Config/config.php";//apikey配置文件
include "______API/JdClient.php";//请求文件
include "______API/request/UnionOpenGoodsJingfenQueryRequest.php";//接口参数
include "______API/request/domain/UnionOpenGoodsJingfenQuery/GoodsReq.php";//接口参数
$c = new JdClient();
$c->appKey = $ApiConfig["AppKey"];
$c->appSecret = $ApiConfig["AppSecret"];
$req = new UnionOpenGoodsJingfenQueryRequest();
$goodsReq= new GoodsReq;
$goodsReq->setEliteId(1);
$req->setGoodsReq($goodsReq);
$resp = $c->execute($req);
print_r($resp);

竟然出错,错误代码400参数错误!调试接口

https://api.jd.com/routerjson?access_token=&app_key=key&method=jd.union.open.goods.jingfen.query&v=1.0&sign=C242FFD55C46C50E172D1FA27DE93F79&360buy_param_json=%7B%22goodsReq%22%3A%7B%22eliteId%22%3A1%7D%7D×tamp=2021-09-08+14%3A17%3A06.166%2B0800

urldecode后

https://api.jd.com/routerjson?access_token=&app_key=key&method=jd.union.open.goods.jingfen.query&v=1.0&sign=C242FFD55C46C50E172D1FA27DE93F79&360buy_param_json={"goodsReq":{"eliteId":1}}×tamp=2021-09-08 14:17:06.166+0800

尝试直接接入参数,不用GoodsReq类

include "______Config/config.php";//apikey配置文件
include "______API/JdClient.php";//请求文件
include "______API/request/UnionOpenGoodsJingfenQueryRequest.php";//接口参数
$c = new JdClient();
$c->appKey = $ApiConfig["AppKey"];
$c->appSecret = $ApiConfig["AppSecret"];
$req = new UnionOpenGoodsJingfenQueryRequest();
$goodsReq= array();
$goodsReq["eliteId"]=1;
$req->setGoodsReq($goodsReq); //$req->setGoodsReq(json_encode($goodsReq)); 原先用json,一样出错!
$resp = $c->execute($req);
print_r($resp);

最后终于调试成功!被官方的示例折腾了一个多小时

博客链接:https://blog.32xp.com/jd-api-php.html

你可能感兴趣的:(PHP学习笔记,php,京东接口,京东API)