首页 > 解决方案 > 无法在邮递员中测试 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。我不知道我在互联网上搜索什么来解决这个问题。请参阅下面的图片,出于安全原因,我更改了网址。

在此处输入图像描述

请告诉我我该怎么做。

标签: phprestpostman

解决方案


您可以在 post 变量中传递方法键。$_REQUEST 可以接受任何请求类型,无论它是 get 类型的 post 类型。所以你只需要在 get 或 post 中传递方法键。如果您想检查请求是发布还是获取,您可以检查如下:

$_SERVER['REQUEST_METHOD']

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
}

希望它可以帮助你。


推荐阅读