php - 无法在邮递员中测试 PHP 方法
问题描述
我在邮递员中测试了许多 REST API,但从未遇到过包含$_REQUEST['method']
决定调用什么方法的 API。现在我的问题是如何在邮递员上测试这个 API。我如何$_REQUEST['method']
在邮递员中传递姓名。
这是我的PHP代码
<?php
include_once('config.php');
if(isset($_REQUEST['method'])){
// echo '<pre>';
if($_POST['method']=='create'){
$name= $_POST['name'];
$location = $_POST['location'];
// $images = null;
$rating = $_POST['rating'];
$specility = $_POST['specility'];
$file = rand(1000,100000)."-".time().'-'.$_FILES['image']['name'];
$file_loc = $_FILES['image']['tmp_name'];
$file_size = $_FILES['image']['size'];
$file_type = $_FILES['image']['type'];
$path_name="images/".$file;
move_uploaded_file($file_loc,$path_name);
$query = "INSERT into `restaurant` (name, location, image, rating,specility) VALUES ('$name', '$location', '$path_name', '$rating','$specility')";
$result = mysqli_query($con,$query);
if($result){
echo json_encode(['status'=>'success','response'=>'Restaurant created successfuly']);
}else{
echo json_encode(['status'=>'failed','response'=>'Restaurant details are not proper']);
}
}
if($_POST['method']=='list'){
$query = "SELECT * FROM `restaurant`";
$result = mysqli_query($con,$query);
if(mysqli_num_rows($result)>0){
$data=mysqli_fetch_assoc($result);
echo json_encode(['status'=>'success','response'=>$data]);
}else{
echo json_encode(['status'=>'failed','response'=>'No data found']);
}
}
}else{
echo json_encode(['status'=>'failed','response'=>'Something went wrong']);
}
我还在服务器上托管了这个 API。我不知道我在互联网上搜索什么来解决这个问题。请参阅下面的图片,出于安全原因,我更改了网址。
请告诉我我该怎么做。
解决方案
您可以在 post 变量中传递方法键。$_REQUEST 可以接受任何请求类型,无论它是 get 类型的 post 类型。所以你只需要在 get 或 post 中传递方法键。如果您想检查请求是发布还是获取,您可以检查如下:
$_SERVER['REQUEST_METHOD']
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
}
希望它可以帮助你。
推荐阅读
- ios - 快速滚动时跳过 UIScrollView 委托函数
- sql - 在 UNION 之后过滤重复项
- python - 如何使用 gitpython 克隆不与 git 关联的 git 存储库?
- hibernate - 休眠和 MySQL
- python - 使用递归绘制嵌套三角形
- c++ - 在 Ubuntu 上使用 libc++ 的 clang 编译器出现问题
- ios - 如何使用 SwiftUI 重置子视图状态变量?
- javascript - 对象值仅在数组的第一个元素中增加
- ruby-on-rails - 为什么 Rails 中的 lib 目录默认不在自动加载路径中?
- flutter - 如何将函数中的数据显示为循环?