Home  >  Article  >  Backend Development  >  Use PHP to connect to the JD Industrial Platform API interface to realize the product attribute query function!

Use PHP to connect to the JD Industrial Platform API interface to realize the product attribute query function!

PHPz
PHPzOriginal
2023-07-07 21:55:35756browse

Use PHP to connect to the JD Industrial Platform API interface to realize the product attribute query function!

On e-commerce platforms, it is often necessary to query product attribute information, such as price, inventory, specifications, etc. The JD Industrial Platform provides a rich API interface to facilitate developers to obtain detailed attribute information of products. This article will introduce how to use PHP to connect to the JD Industrial Platform API to implement the product attribute query function.

  1. Preparation
    Before using the JD Industrial Platform API, you need to apply for a developer account and obtain the API’s appkey, appsecret and other information. This information can be obtained on the open platform of JD Industrial Platform.
  2. PHP code example
<?php

// 定义API请求的URL地址
$url = 'https://api.jd.com/routerjson';

// 设置请求的参数
$params = array(
    'method' => 'jingdong.ware.baseproduct.get',
    'access_token' => '',  // 这里填写你的access_token
    'app_key' => '',  // 这里填写你的app_key
    'app_secret' => '',  // 这里填写你的app_secret
    'timestamp' => date('Y-m-d H:i:s'),
    'format' => 'json',
    'v' => '2.0',
    'param_json' => '{"skuId":"123456"}',  // 这里填写要查询的商品skuId
);

// 对参数进行排序
ksort($params);

// 生成签名
$signStr = '';
foreach ($params as $key => $value) {
    $signStr .= $key . $value;
}
$sign = strtoupper(md5($signStr));

// 添加签名到参数数组中
$params['sign'] = $sign;

// 发送HTTP请求获取商品属性信息
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$result = curl_exec($ch);
curl_close($ch);

// 处理返回结果
$result = json_decode($result, true);
if (isset($result['result']['success']) && $result['result']['success'] == true) {
    $productInfo = $result['result']['productInfo'];
    echo '商品名称:' . $productInfo['name'] . "
";
    echo '商品价格:' . $productInfo['goodsPrice'] . "
";
    echo '商品库存:' . $productInfo['stockNum'] . "
";
} else {
    echo '查询商品属性失败!';
}

?>

The above code sends an HTTP request, passes the parameters to the JD Industrial Platform API, obtains the returned product attribute information, and processes and displays it.

  1. Run the sample code
    Before running the sample code, you need to complete the access_token, app_key, app_secret and the product skuId to be queried in the code. Then save the code as a PHP file and run the file through a browser or command line tool.

The parameter method in the sample code is the API interface name for querying the basic attributes of the product, and skuId in param_json is the query The skuId of the product. You can query the API interfaces and parameters of other attributes according to your actual needs.

Summary
This article introduces how to use PHP to connect to the JD Industrial Platform API interface to realize the product attribute query function. By sending an HTTP request and passing the parameters to the API interface, the returned product attribute information is obtained, processed and displayed. Developers can expand and optimize the code according to actual needs to achieve more functions.

The above is the detailed content of Use PHP to connect to the JD Industrial Platform API interface to realize the product attribute query function!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn