我想建立一个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
什么是真正的地狱? –