2017-05-08 101 views
0

我想了解使用REST服務的基礎知識。我是following along to this example並試圖讓它與我的sql數據庫一起工作。我將我的api.php文件關閉了一些在註釋中重寫的代碼。我知道只有我的client.php頁面才能被網頁瀏覽,但在瀏覽器中查看其中的任何一個文件時,我都會看到一個空白頁面。一切工作正常,當值是硬編碼像在例子中,但是當試圖從數據庫調用值似乎打破。REST調用返回空白頁

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

嘗試使用$ _REQUEST而不是$ _GET希望這將幫助你

+0

可惜還是來了空白 –