我试图了解使用REST服务的基础知识。我是following along to this example并试图让它与我的sql数据库一起工作。我的api.php文件基于评论中的一些重写代码。我知道只有我的client.php页面应该被网络浏览,但是当我在浏览器中查看它们中的任何一个文件时,我得到一个空白页面。当值如示例中那样硬编码时,一切正常,但是当尝试从数据库中调用值时,它似乎会中断。
client.php
<html>
<body>
<?php
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] ==
"get_app")
{
$app_info = file_get_contents('http://localhost:8888/api.php?
action=get_app&id=' . $_GET["id"]);
$app_info = json_decode($app_info, true);
?>
<table>
<tr>
<td>App Name: </td><td> <?php echo $app_info["app_name"] ?></td>
</tr>
<tr>
<td>Price: </td><td> <?php echo $app_info["app_price"] ?></td>
</tr>
<tr>
<td>Version: </td><td> <?php echo $app_info["app_version"] ?></td>
</tr>
</table>
<br />
<a href="http://localhost:8888/client.php?action=get_app_list" alt="app
list">Return to the app list</a>
<?php
}
else // else take the app list
{
$app_list = file_get_contents('http://localhost:8888/api.php?
action=get_app_list');
$app_list = json_decode($app_list, true);
?>
<ul>
<?php
foreach ((array) $app_list as $app): ?>
<li>
<a href=<?php echo "http://localhost:8888/client.php?
action=get_app&id=" . $app["id"] ?> alt=<?php echo "app_" . $app_["id"] ?>>
<?php echo $app["name"] ?></a>
</li>
<?php endforeach;
?>
</ul>
<?php
} ?>
</body>
</html>
api.php
<?php
/*DB settings*/
ini_set('display_errors',1);
error_reporting(E_ALL);
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "");
define("MYSQL_DATABASE", "test");
$results=array();
$result=array();
function get_app_by_id($id)
{
$sql = "SELECT * from apps where id=$id";
try {
$dbh = new
PDO("mysql:host=".MYSQL_HOST.";dbname=".MYSQL_DATABASE.";charset=utf8",
MYSQL_USER, MYSQL_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
} catch(PDOException $e) {
}
$app_info = $result;
return $app_info;
}
function get_app_list()
{
$sql = "SELECT * FROM apps";
try {
$dbh = new
PDO("mysql:host=".MYSQL_HOST.";dbname=".MYSQL_DATABASE.";charset=utf8",
MYSQL_USER, MYSQL_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query($sql);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
} catch(PDOException $e) {
}
$app_list = $results;
return $app_list;
}
?>
答案 0 :(得分:0)
尝试使用$ _REQUEST而不是$ _GET希望这会对你有所帮助