我想建立一個JSON輸出API,第二個功能是產生所需的輸出,但我不能從網絡調用它。而第一個函數按預期返回大量文本。我是否在使用不兼容的東西?我在Ubuntu 14.04服務器上使用PHP 5.5.9版本。PHP功能不可瀏覽
我可以在終端和瀏覽器中查看這個函數的結果;
<?php
class ArticlesAPI {
function top() {
$db = new mysqli("mysql-host.rds.amazonaws.com", "user", "password", "db_name");
$results = $db->query("SELECT article_id, title, summary FROM top_articles");
while ($row = $results->fetch_assoc()) {
echo $row['article_id'];
echo $row['title'];
echo $row['summary'];
}
$results->close();
}
}
$api = new ArticlesAPI;
$api->top();
?>
該函數只返回終端中的結果;
<?php
class ArticleAPI {
function top() {
$db = new mysqli("mysql-host.rds.amazonaws.com", "user", "password", "db_name");
$results = $db->query("SELECT article_id, title, summary FROM top_articles");
$articles = array();
while($article = $results->fetch_assoc()){
$article_id = $article['article_id'];
$articles[$article_id][] = $article['title'];
$articles[$article_id][] = $article['summary'];
}
$results->close();
$db->close();
$json = json_encode($articles);
echo $json;
}
}
$api = new ArticleAPI;
$api->top();
?>
在網絡上運行時,從第一個塊輸出什麼樣的文本? (請記住,在發佈之前從輸出中刪除任何實時數據) –
http://ec2-54-152-162-157.compute-1.amazonaws.com/mysql_test.php這是目前的隨機數據而非實時數據。 – Rhys
什麼是真正的地獄? –