Java 完整调用示例(HttpClient)
依赖 Maven
xml
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<!-- commons-codec MD5 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
核心代码
java
运行
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.util.*;
public class JdGoodsDetailApi {
// 替换为自己的密钥
private static final String APP_KEY = "你的appKey";
private static final String APP_SECRET = "你的appSecret";
private static final String API_URL = "https://api.jd.com/routerjson";
// 商品详情接口方法
private static final String METHOD = "jd.union.open.goods.jingfen.query";
public static void main(String[] args) throws Exception {
// 1.业务入参,skuId为京东商品SKU编号
Map<String, Object> bizParam = new HashMap<>();
bizParam.put("skuId", 100012345678); // 目标商品SKU
bizParam.put("goodsType", 0);
// 2.组装全部参数
Map<String, String> allParams = new TreeMap<>();
allParams.put("app_key", APP_KEY);
allParams.put("timestamp", new Date().toLocaleString());
allParams.put("v", "1.0");
allParams.put("method", METHOD);
allParams.put("format", "json");
// 业务参数转JSON字符串
allParams.put("param_json", new com.alibaba.fastjson.JSONObject(bizParam).toString());
// 3.生成MD5签名
String sign = getSign(allParams);
allParams.put("sign", sign);
// 4.POST请求
String result = doPost(allParams);
System.out.println("接口返回结果:" + result);
}
// 生成签名
private static String getSign(Map<String, String> params) {
StringBuilder sb = new StringBuilder(APP_SECRET);
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append(entry.getValue());
}
sb.append(APP_SECRET);
return DigestUtils.md5Hex(sb.toString()).toUpperCase();
}
// 发送Post请求
private static String doPost(Map<String, String> params) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(API_URL);
httpPost.setEntity(new StringEntity(new com.alibaba.fastjson.JSONObject(params).toString(), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = httpClient.execute(httpPost);
String resp = EntityUtils.toString(response.getEntity(), "UTF-8");
response.close();
httpClient.close();
return resp;
}
}
三、PHP 调用实例
php
运行
<?php
$appKey = '你的appKey';
$appSecret = '你的appSecret';
$method = 'jd.union.open.goods.jingfen.query';
$url = 'https://api.jd.com/routerjson';
// 业务参数
$bizData = [
'skuId' => 100012345678,
'goodsType' => 0
];
$paramJson = json_encode($bizData);
// 公共参数
$params = [
'app_key' => $appKey,
'timestamp' => date('Y-m-d H:i:s'),
'v' => '1.0',
'method' => $method,
'format' => 'json',
'param_json' => $paramJson
];
// 字典排序
ksort($params);
$str = $appSecret;
foreach ($params as $k => $v) {
$str .= $k . $v;
}
$str .= $appSecret;
$sign = strtoupper(md5($str));
$params['sign'] = $sign;
// curl请求
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
echo $res;
?>
四、返回核心字段说明(联盟详情接口)
成功返回 JSON 核心节点:
jingfenGoodsInfo商品主体信息- skuId:商品 ID
- skuName:商品标题
- price:售价、originPrice:原价
- mainImage:主图地址,galleryImages:图集
- brandName:品牌,category:类目
couponInfo优惠券信息(面额、门槛、链接)commissionInfo佣金比例(联盟推广佣金)