dtdr57046 2017-05-08 16:39 采纳率: 100%
浏览 120

REST调用返回空白页面

I am trying to get the basics down of using REST services. I am following along to this example and trying to get it to work with my sql database. I am basing my api.php file off some of the re written code in the comments. I understand that only my client.php page should be getting viewed by the web, but I get a blank page when viewing either of the files them in the browser. Everything works fine when the values are hardcoded like in the example, but when trying to call the values from a database it seems to break.

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;
}

?>
  • 写回答

1条回答 默认 最新

  • doupo2241 2017-05-08 17:07
    关注

    Try to use $_REQUEST instead of $_GET hope this will help you

    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法