gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:17:48 25 4
gpt4 key购买 nike

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

以下是托管版本的 URL: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

最佳答案

已修复!

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

关于PHP get 端点在本地工作,但在服务器上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46869694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com