2017-10-22 58 views
0

我已经在PHP中编写了一个端点来查询mysql数据库并以JSON格式返回结果 - 它在本地工作,但在部署到AWS时无法工作。PHP获取端点在本地工作,但不在服务器上

这里有一个托管版本的网址: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网关超时,但本地主机完美的作品。

Json displayed on localhost

504 Gateway on hosted version

+0

您是否测试过托管版本和数据库之间的连接,以及占用您本地版本执行查询的时间与托管版本的时间? – pah

+0

我没有测试托管到数据库的连接,因为我无法加载页面(我将在一秒内创建一个简化的数据库连接php页面)。但我认为这个问题很可能与AWS的权限有关......我也在调查。 –

+0

你在错误日志中有什么? –

回答

0

固定!

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

相关问题