当前位置: 首页 » 笔记 » 京东联盟新版API接口PHP版的坑

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

零分笔记1,6312022-07-28 10:35

京东联盟新版API接口的坑,PHP版,其他版本不会!

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

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

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

合并下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=27cb4b7af2e42be76ed0a395b6584bbc&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=27cb4b7af2e42be76ed0a395b6584bbc&method=jd.union.open.goods.jingfen.query&v=1.0&sign=C242FFD55C46C50E172D1FA27DE93F79&360buy_param_json={"goodsReq":{"eliteId":1}}&timestamp=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);

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

END
零分站龄5个月资深站长
一个喜欢折腾,却又折腾不出像样东西的,不会PHP的PHP程序员!
324
文章
8
分类
126
标签
2
友链
onlinelovesky317355746vipsever@vip.qq.com