一、引言
在电商、仓储、零售等系统开发中,我们经常需要通过商品条码(如EAN-13、UPC)来获取商品名称、规格、品牌等信息。手动维护这些数据效率低,还容易出错。
这时,很多开发者会选择接入一个商品条码查询API接口,通过条码自动获取商品基础信息。本文将介绍如何使用 PHP 调用这类接口,实现条码到商品信息的自动化查询。
二、调用示例
下面我们以探数API在阿里云的接口代码为例
接口地址:https://market.aliyun.com/detail/cmapi00065867
<?php
$host = "https://tsbarcode.market.alicloudapi.com";
$path = "/barcode/index";
$method = "GET";
$appcode = "你自己的AppCode";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "barcode=6921830106820";
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
var_dump(curl_exec($curl));
?>