调用苏宁商品详情 API 的核心是通过 HTTP/HTTPS 协议发送请求并处理响应,因此几乎所有主流编程语言都支持,只要该语言能实现网络请求(如 GET/POST)和数据解析(如 JSON)。以下是几种常见语言的示例:
- JavaScript(Node.js)
通过 axios 或内置 http 模块发送请求,适合前端或后端脚本:
javascript
const axios = require('axios'); // 需先安装:npm install axios
async function getSuningProduct(productId, apiKey) {
try {
const url = http://api.suning.com/api/products/details;
const response = await axios.get(url, {
params: {
product_id: productId,
api_key: apiKey
}
});
const data = response.data;
console.log('商品名称:', data.data.name);
console.log('价格:', data.data.price);
} catch (error) {
console.error('请求失败:', error.response?.status || error.message);
}
}
// 调用示例
getSuningProduct('12345', 'your_api_key');
- Java
使用 HttpURLConnection 或第三方库(如 OkHttp),适合企业级应用:
java
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser; // 需引入Gson库解析JSON
public class SuningApiDemo {
public static void main(String[] args) throws Exception {
String productId = "12345";
String apiKey = "your_api_key";
String url = "http://api.suning.com/api/products/details?product_id=" + productId + "&api_key=" + apiKey;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()./service/https://developer.aliyun.com/url(url).build();
try (Response response = client.newCall(request).execute()) {
String jsonData = response.body().string();
JsonObject data = JsonParser.parseString(jsonData).getAsJsonObject().get("data").getAsJsonObject();
System.out.println("商品名称:" + data.get("name").getAsString());
System.out.println("价格:" + data.get("price").getAsString());
}
}
}
- PHP
适合 Web 开发场景,使用 file_get_contents 或 curl:
php
<?php
$productId = "12345";
$apiKey = "your_api_key";
$url = "http://api.suning.com/api/products/details?product_id={$productId}&api_key={$apiKey}";
$response = file_get_contents($url);
if ($response) {
$data = json_decode($response, true);
echo "商品名称:" . $data['data']['name'] . "\n";
echo "价格:" . $data['data']['price'] . "\n";
} else {
echo "请求失败";
}
?>
- C#
通过 HttpClient 发送请求,适合.NET 平台:
csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string productId = "12345";
string apiKey = "your_api_key";
string url = $"http://api.suning.com/api/products/details?product_id={productId}&api_key={apiKey}";
using (HttpClient client = new HttpClient()) {
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode) {
string json = await response.Content.ReadAsStringAsync();
// 解析JSON(可使用Newtonsoft.Json库)
dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
Console.WriteLine($"商品名称:{data.data.name}");
Console.WriteLine($"价格:{data.data.price}");
} else {
Console.WriteLine($"请求失败,状态码:{response.StatusCode}");
}
}
}
}
- Ruby
通过 net/http 或 faraday 宝石发送请求:
ruby
require 'net/http'
require 'json'
product_id = "12345"
api_key = "your_api_key"
url = URI.parse("http://api.suning.com/api/products/details?product_id=#{product_id}&api_key=#{api_key}")
response = Net::HTTP.get_response(url)
if response.is_a?(Net::HTTPSuccess)
data = JSON.parse(response.body)
puts "商品名称:#{data['data']['name']}"
puts "价格:#{data['data']['price']}"
else
puts "请求失败:#{response.code}"
end
总结
无论使用哪种语言,核心步骤一致:
构造包含商品 ID、API 密钥等参数的请求 URL;
发送 HTTP GET(或 POST,根据 API 要求)请求;
解析返回的 JSON(或 XML)响应数据。
实际使用时,需参考苏宁开放平台的最新文档,确认请求方式、参数格式(如是否需要签名验证)及响应结构,确保兼容性。