PHP get端点在本地工作,但不在服务器上工作

时间:2017-10-22 01:33:50

标签: php mysql

我在PHP中编写了一个端点来查询mysql数据库并以JSON格式返回结果 - 它在本地工作,但在我部署到AWS时却没有。

以下是托管版本的网址:http://www.everythingproduct.com/getArticlesTest.php?type=All

这是代码;

<?php

header('Content-Type: application/json');

ini_set('display_errors', 'On');
error_reporting(E_ALL);

// Assign the post values to variables
// Types of request - E.g. 'All', 'Article' or a 'Tag-Search'
$type = $_GET["type"];

// If a specific article, what article?
$article  = $_GET["article"];

// If a tag-search, what tag used?
$tag  = $_GET["tag"];

// Define database parameters
$servername = "***********.cl8bq5gds2sn.us-east-2.rds.amazonaws.com:3306";
$username = "********";
$password = "********";
$dbname = "*********";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

If ($type == 'All') {
    $sql = "SELECT * FROM articles ORDER BY datetime desc LIMIT 10";
    $result = $conn->query($sql);
} elseif ($type == 'Article') {
    $sql = "SELECT * FROM articles WHERE id = " . $article;
    $result = $conn->query($sql);
} elseif ($type == 'Tag-search') {
    $sql = "SELECT * FROM articles WHERE tags like '%" . $tag . "%' ORDER BY datetime desc LIMIT 10";
    $result = $conn->query($sql);
}

mysqli_close($conn);

// Put the results into an array, and echo some json

$resultArray = array();
while($row = mysqli_fetch_assoc($result)) {
    array_push($resultArray, array(
            'id' => $row["id"],
            'datetime' => $row["datetime"],
            'source' => utf8_encode($row["source"]),
            'title' => utf8_encode($row["title"]),
            'description' => utf8_encode($row["description"]),
            'img' => $row["img"],
            'link' => $row["link"],
            'tags' => $row["tags"]
        )
    );
}


$resultArray = json_encode($resultArray);
echo $resultArray;

?>

忽略明显的安全性内容(我知道),我无法理解为什么我只是从托管版本获得504网关超时,但localhost工作正常。

Json displayed on localhost

504 Gateway on hosted version

1 个答案:

答案 0 :(得分:0)

固定!

必须更改与RDS关联的安全组的AWS入站规则,以接受来自EC2实例的流量。