Php 为什么这个JSON数组看起来是空的?
我正在使用Web服务同步来自我们分销商的产品。作为第一步,我构建了一个表来显示数据,以验证一切都正常工作。我已能够使用以下功能成功检索产品说明:Php 为什么这个JSON数组看起来是空的?,php,json,web-services,Php,Json,Web Services,我正在使用Web服务同步来自我们分销商的产品。作为第一步,我构建了一个表来显示数据,以验证一切都正常工作。我已能够使用以下功能成功检索产品说明: function get_description($api_key, $item){ $url = 'http://www.stl-distribution.com/webservices/json/GetProductDescription.php'; $post_vars = 'api_key='.$api_key.'&item='.$it
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;
}
Notice: Undefined index: product_type in C:\xampp\htdocs\STLImport\view\user_form.php on line 21
我使用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\u meta\u data()
函数的代码如下:
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;
}
当我在返回$json\u数组之前执行print\u r()
时代码>语句我得到以下错误:
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"
}
似乎我的get\u meta\u data()
函数中有一个错误。我的API提供商告诉我,这一行
$post_vars = 'api_key='.$api_key.'&item='.$item;
<td class="center"><?php $item_data = get_meta_data($api_key,$item[0]); echo $item_data['product_type']; ?>
应该是
$post_vars = 'api_key='.$api_key.'&items='.$item;
<td class="center"><?php $item_data = get_meta_data($api_key,$item[0]); echo $item_data[$item[0]]['product_type']; ?></td>
注意添加了一个“s”。因此,$json_数组
显示为空,因为它是空的。我的另一个函数可以工作,因为它不需要像getProductMetabase
那样的's'
另外,这条线
$post_vars = 'api_key='.$api_key.'&item='.$item;
<td class="center"><?php $item_data = get_meta_data($api_key,$item[0]); echo $item_data['product_type']; ?>
应该是
$post_vars = 'api_key='.$api_key.'&items='.$item;
<td class="center"><?php $item_data = get_meta_data($api_key,$item[0]); echo $item_data[$item[0]]['product_type']; ?></td>
因为,正如示例数据所暗示的,数组是多维的。所需的值(产品类型
)包含在$item[0]
数组中。因此,$item\u data['product\u type']
不存在。在终端中使用“curl”功能测试您的请求。也许这会给您提供更多信息。在执行json\u decode()
之前,您是否查看了输出字符串?同意@SDC。您正在从分发服务器获取JSON,但由于某些原因,您的项目ID无法返回任何内容。您的参数名称拼写是否正确,大小写是否正确?@SDC@J.D.Pace是查看输出的最佳方式?我试过了,收到了错误[{“error”:“\”\“notfound”}]
好的。现在,请尝试打印$url
,以查看您实际调用的url。这可能会告诉你问题所在。如果你看不到问题,复制并粘贴到你的浏览器中,看看它是否通过直接调用抛出相同的错误。