我向我的PHP文件发送POST请求时遇到问题。每当我使用Chrome的Advanced Rest Client Application测试我的API时,它都无法在$ _POST中收到所需的参数。
我发出请求的网址是smtbursatil.com/api/rate.php,在我写的有效负载中{" disco":" 1",& #34;评分":" 5"},但它得到"失败"响应在此代码中:
<?php
if(isset($_POST['disco']) && isset($_POST['rating'])) {
require_once 'credentials.php';
$connection = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DATABASE);
$disco = mysqli_real_escape_string($connection, $_POST['disco']);
$rating = mysqli_real_escape_string($connection, $_POST['rating']);
$query = 'INSERT INTO ratings (disco, rating) VALUES ('. $disco .', '. $rating .')';
$result = mysqli_query($connection, $query);
echo json_encode($result);
mysqli_close($connection);
} else
echo "Failed";
?>
我在同一个网站(smtbursatil.com/contacto.php)中也有一个联系表格,用以下代码调用(smtbursatil.com/includes/contact.php):
<?php
if(isset($_POST['csubmit'])) {
$sendto = 'admin@smtbursatil.com';
$subject = 'Consulta';
$headers = 'From: '.htmlentities($_POST['cname']).' <'.htmlentities($_POST['cmail']).'>'."\r\n";
$headers .='Reply-To: '.$sendto. "\r\n" ;
$headers .='X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($sendto, $_POST['csubject'], $_POST['cmessage'], $headers);
}
header('Location: ../contacto');
?>
这个要求它完美无缺。那么这可能有什么问题呢? API调用没有填充$ _POST对象,但联系表单工作正常,两个文件位于同一主机中(一个在/ api子文件夹中,另一个PHP文件位于/ includes)。