【问题标题】:Unable to test PHP methods in postman无法在邮递员中测试 PHP 方法
【发布时间】:2019-02-13 11:16:21
【问题描述】:

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

请告诉我该怎么做。

【问题讨论】:

  • 你能显示你的请求参数吗?
  • 哪些参数?
  • 请求参数
  • 在脚本中根据需要添加值为“create”或“list”的键“method”。
  • 我应该在正文部分下的form-data 中添加它

标签: php rest postman


【解决方案1】:

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

$_SERVER['REQUEST_METHOD']

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2021-05-12
    • 2019-05-14
    • 1970-01-01
    • 2022-01-25
    • 2018-10-12
    • 2017-02-21
    相关资源
    最近更新 更多