我正在使用web服务来同步我们分销商的产品。作为第一步,我建立了一个表格来显示数据以验证一切正常。我已经能够使用下面的函数成功检索产品说明:为什么这个JSON数组显示为空?
function get_description($api_key, $item){
$url = 'http://www.stl-distribution.com/webservices/json/GetProductDescription.php';
$post_vars = 'api_key='.$api_key.'&item='.$item;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_vars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$return_data = curl_exec($ch);
$json_array = json_decode($return_data,true);
return $json_array;
}
我使用foreach循环如下显示的信息:
<table>
<tr>
<th>ISBN</th>
<th>Description:</th>
<th>Categories:</th>
<th>Options:</th>
</tr>
<?php foreach($product_ISBNs as $item) : ?>
<tr>
<td class="center"><?php echo $item[0]?></td>
<td class="center"><?php $item_description = get_description($api_key,$item[0]); echo $item_description['description'];?>
<td class="center"><?php $item_data = get_meta_data($api_key,$item[0]); echo $item_data['product_type']; ?>
</tr>
<?php endforeach?>
</table>
虽然我已成功能够检索说明使用get_description()
功能,我一直从get_meta_data()
功能得到以下错误:
Notice: Undefined index: product_type in C:\xampp\htdocs\STLImport\view\user_form.php on line 21
为get_meta_data()
函数的代码是followss:
function get_meta_data($api_key, $item){
$url = 'http://www.stl-distribution.com/webservices/json/GetProductMetaBasic.php';
$post_vars = 'api_key='.$api_key.'&item='.$item;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_vars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$return_data = curl_exec($ch);
$json_array = json_decode($return_data,true);
return $json_array;
}
当我做了print_r()
立即return $json_array;
声明我收到以下错误之前:
Array ([0] => Array ([error] => "" not found))
根据我们的分销商的网站,我们的要求是被发送和接收。每次刷新页面时,我的使用统计数据都会上升。所以,似乎没有数据被返回,或者我没有正确引用它。我知道这些产品存在于数据库中,因为它正在返回描述。因此,我不能正确引用它,但我无法找到我的错误所在。我曾尝试使用各种组合来引用它,但无济于事。 WebService的文档提供了这个例子中的数据应该如何返回:
{
"9781434768513":
{
"isbn13":"9781434768513",
"isbn":"1434768511",
"upc":"000000912992",
"title":"Crazy Love",
"subtitle":"Overwhelmed By A Relentless God",
"contributor1":"Chan, Francis",
"contributor2":"Yankoski, Danae",
"contributor3":"",
"vendor":"David C. Cook",
"release_date":"20080430",
"retail":"14.99",
"binding":"Paperback",
"product_type":"Books",
"category1":"Christian Living",
"category2":"",
"category3":"",
"grade_level_start":"",
"grade_level_end":"",
"inventory_updated":"20100825 11:25",
"tn_available":993,
"tn_onorder":240,
"nv_available":735,
"nv_onorder":0,
"image_small":"http:\/\/www.stl-distribution.com\/covers\/7814\/sm-9781434768513.jpg",
"image_medium":"http:\/\/www.stl-distribution.com\/covers\/7814\/md-9781434768513.jpg",
"image_large":"http:\/\/www.stl-distribution.com\/covers\/7814\/lg-9781434768513.jpg"
}
用终端中的'curl'函数测试您的请求。也许这会给你更多的信息。 – Reflic 2013-03-19 16:24:33
你在查看'json_decode()'之前的输出字符串吗? – SDC 2013-03-19 16:26:13
同意@SDC。您从分销商处获得JSON,但出于某种原因,您的商品ID无法返回任何内容。你的参数名拼写是否正确,并且是正确的? – 2013-03-19 16:31:57