Home > Article > Backend Development > Use PHP to connect to the JD Industrial Platform API interface to realize the price query function!
Use PHP to connect to the JD Industrial Platform API interface to realize the price query function!
Jingdong Industrial Platform (API) is a set of open platform interfaces provided by Jingdong Mall for merchants. During the development process, various functions, including price inquiries, can be implemented by calling the API interface.
First, you need to apply for and obtain the API key of the JD Industrial Platform. The API key contains important information for accessing the API interface of the JD Industrial Platform.
Next, we use PHP to write code to implement the price query function. First, we need to write a class to process API requests and parameters. The code is as follows:
<?php class JdApi { private $appKey; // 申请的API密钥中的appKey private $appSecret; // 申请的API密钥中的appSecret public function __construct($appKey, $appSecret) { $this->appKey = $appKey; $this->appSecret = $appSecret; } public function getPrice($sku) { $url = 'https://api.jd.com/routerjson'; // API接口地址 $method = 'jingdong.price.read.queryPriceInfo'; // API接口方法名 $timestamp = date('Y-m-d H:i:s'); // 当前时间戳 $params = array( 'app_key' => $this->appKey, 'method' => $method, 'timestamp' => $timestamp, 'v' => '2.0', 'sku' => $sku, 'signMethod' => 'md5', 'format' => 'json', 'sign' => '', ); // 生成签名 $sign = $this->generateSign($params); $params['sign'] = $sign; // 发起API请求 $result = $this->curlPost($url, $params); return $result; } private function generateSign($params) { ksort($params); // 参数按键名排序 $str = $this->appSecret; foreach ($params as $key => $value) { $str .= "$key$value"; } $str .= $this->appSecret; $sign = strtoupper(md5($str)); // 生成大写的md5签名 return $sign; } private function curlPost($url, $params) { // 将参数拼接成GET请求的URL $url .= '?' . http_build_query($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $result = curl_exec($ch); curl_close($ch); return $result; } } ?>
The JdApi class in the above code encapsulates the API request method and parameter processing method. In the getPrice($sku) method, we call the API's price query interface. It should be noted that the $url, $method, and other parameters here need to be modified according to the specific API interface document.
Next, we can instantiate the JdApi class elsewhere and call the getPrice method to query the price. The code example is as follows:
<?php $appKey = 'your_app_key'; $appSecret = 'your_app_secret'; $jdApi = new JdApi($appKey, $appSecret); $sku = '123456'; // 要查询价格的商品SKU $result = $jdApi->getPrice($sku); // 处理查询结果 $jsonData = json_decode($result, true); if ($jsonData && isset($jsonData['jingdong_price_read_queryPriceInfo_responce']) && isset($jsonData['jingdong_price_read_queryPriceInfo_responce']['result'])) { $price = $jsonData['jingdong_price_read_queryPriceInfo_responce']['result']['price']; echo "价格: $price 元"; } else { echo "查询失败"; } ?>
Replace $appKey and $appSecret in the above code with In the appKey and appSecret of the API key you applied for, $sku is the product SKU whose price you want to check. The query results get the price by parsing the JSON data and output it to the page.
Through the above code examples, we can use PHP to connect to the JD Industrial Platform API interface to realize the price query function. In actual development, other methods can also be written as needed 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 price query function!. For more information, please follow other related articles on the PHP Chinese website!