如何对 UPC 数据库进行 PHP XML/RPC 调用
干草我正在研究条形码阅读器项目,当我从我的 php 脚本调用 upcdatabase 时,它给了我错误。我使用 www.upcdatabase.com 提供的 php 示例,
代码是
<?php error_reporting(E_ALL);
ini_set('display_errors', true);
require_once 'XML/RPC.php';
$rpc_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Set your rpc_key here
$upc='0639382000393';
// Setup the URL of the XML-RPC service
$client = new XML_RPC_Client('/xmlrpc', 'http://www.upcdatabase.com');
$params = array( new XML_RPC_Value( array(
'rpc_key' => new XML_RPC_Value($rpc_key, 'string'),
'upc' => new XML_RPC_Value($upc, 'string'),
), 'struct'));
$msg = new XML_RPC_Message('lookup', $params);
$resp = $client->send($msg);
if (!$resp)
{
echo 'Communication error: ' . $client->errstr;
exit;
}
if(!$resp->faultCode())
{
$val = $resp->value();
$data = XML_RPC_decode($val);
echo "<pre>" . print_r($data, true) . "</pre>";
}else{
echo 'Fault Code: ' . $resp->faultCode() . "\n";
echo 'Fault Reason: ' . $resp->faultString() . "\n";
}
?>
当我检查 $upc='0639382000393' 时;进入upc数据库查看这个然后它工作正常但我在浏览器中运行这个脚本然后它给出出现以下错误
Array ( [status] => fail [message] => Invalid UPC length )
hay i am working on barcode reader project when i call upcdatabase from my php script it give me errors. i use the php example provided by www.upcdatabase.com
the code is
<?php error_reporting(E_ALL);
ini_set('display_errors', true);
require_once 'XML/RPC.php';
$rpc_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Set your rpc_key here
$upc='0639382000393';
// Setup the URL of the XML-RPC service
$client = new XML_RPC_Client('/xmlrpc', 'http://www.upcdatabase.com');
$params = array( new XML_RPC_Value( array(
'rpc_key' => new XML_RPC_Value($rpc_key, 'string'),
'upc' => new XML_RPC_Value($upc, 'string'),
), 'struct'));
$msg = new XML_RPC_Message('lookup', $params);
$resp = $client->send($msg);
if (!$resp)
{
echo 'Communication error: ' . $client->errstr;
exit;
}
if(!$resp->faultCode())
{
$val = $resp->value();
$data = XML_RPC_decode($val);
echo "<pre>" . print_r($data, true) . "</pre>";
}else{
echo 'Fault Code: ' . $resp->faultCode() . "\n";
echo 'Fault Reason: ' . $resp->faultString() . "\n";
}
?>
when i check the $upc='0639382000393'; into upc data base view this then it works fine but i run this script into the browser then it give the following error
Array ( [status] => fail [message] => Invalid UPC length )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,他们的 API 文档似乎相当简短。
该网站在 商品查找页面上提到了三种类型的代码:
在页面提到这三种类型之后,它还说,
13 位 EAN/UCC-13 是 UPC。它包含有效的 UPC,但还有许多其他不是有效 UPC 的值。
来自有关 EAN-13 的维基百科文章:
话虽如此,当我从
$upc
中删除前导零时,它按预期工作。显然,“项目查找”页面具有删除前导零的逻辑,而 API 则没有。或者,您可以将原始 13 位值设置为
ean
参数,而不是设置upc
参数,它也可以工作。Unfortunately, their API appears rather short on documentation.
There are three types of codes the site mentions on the Item Lookup page:
Right after the page mentions those three types, it also says,
The 13-digit EAN/UCC-13 is a superset of UPC. It includes valid UPCs, but it has many other values that are not valid UPCs.
From the Wikipedia article on EAN-13:
Having said that, when I removed the leading zero from
$upc
, it worked as expected. Apparently the Item Lookup page has logic to remove the leading zero, while the API does not.Alternatively, instead of setting the
upc
param, you can set the original 13-digit value to theean
param and it will also work.