【发布时间】:2014-04-17 17:04:21
【问题描述】:
我是编写 Web 服务的新手,我似乎无法弄清楚这一点!我有一个简单的 php restful 服务,它返回一个 json 响应,但我在响应中没有任何键(我不知道这是否是正确的术语)我得到的是这样的。
(
(
"name",
qty,
"image",
"price",
"date"
)
我很肯定我在这里做错了。
这是我的服务:
<?php
header('Content-type: application/json');
class InventoryAPI {
private $db;
function __construct() {
$this->db = new mysqli('localhost', 'user', 'pass', 'dbname');
$this->db->autocommit(FALSE);
}
function __destruct() {
$this->db->close();
}
function checkInv() {
$query = "SELECT model, sku, quantity, stock_status_id, image, price, date_available FROM table_name";
$result = $this->db->query($query);
$rows = array();
$i = 0;
while($row = $result->fetch_row())
{
$rows[] = $row;
$i++;
}
$result->close();
$this->db->close();
echo json_encode($rows);
}
}
$api = new InventoryAPI;
$api->checkInv();
?>
【问题讨论】:
标签: php json web-services rest