我正在使用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