简体   繁体   English

如果我在API中使用POST方法,如何通过在URL中传递值来在浏览器中检查php API?

[英]How to check php API in browser by passing values in url if I'm using POST method in API?

This API working fine when I'm testing on Postman but when I'm trying to check in browser by passing id in url it showing error. 当我在Postman上进行测试时,此API可以正常工作,但是当我尝试通过在URL中传递id来检查浏览器时,它显示错误。 http://192.168.43.88/mydatabase/delete.php?id=1 when I use GET method then then my able to pass value in url but why m getting error in case of POST method. http://192.168.43.88/mydatabase/delete.php?id=1,当我使用GET方法时,然后我可以在url中传递值,但是为什么在POST方法的情况下会出错。

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id = $_POST['id'];
    if($id == '') {
        echo 'please provide required value';
    } else {
        require_once('connection.php');
        $sql = "SELECT * FROM circle_detail WHERE id='$id' AND deleted='0' ";
        $check = mysqli_fetch_array(mysqli_query($conn, $sql));
        if(isset($check)) {
            $sql = "UPDATE  circle_detail SET deleted='1' WHERE id='$id'";
            if(mysqli_query($conn, $sql)) {
                echo 'marked as deleted';
            } else {
                echo 'oops! Please try again!';
            } 
        } else { 
            echo 'not found';
        }
        mysqli_close($conn);
    }
} else {
    echo 'error';
}

If you are using the POST request method but passing the variable in as a GET parameter then you need to change the following code: 如果您使用的是POST请求方法,但将变量作为GET参数传递进来,则需要更改以下代码:

$id = $_POST['id'];

To be 成为

$id = $_GET['id'];

This way when it is a POST request it'll still look for the get parameter. 这样,当它是POST请求时,它仍将寻找get参数。 Where as it is currently looking for a POST variable called id 目前正在哪里寻找名为id的POST变量

If you want to use GET and POST you remove 如果要使用GET和POST,请删除

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

and use 和使用

$_REQUEST['id'] $ _REQUEST [ '身份证']

instead of $_POST 而不是$ _POST

You can not call POST Method API from the browser for that you should use "Postman". 您不能从浏览器调用POST方法API,因为您应该使用“邮递员”。 Postman is the complete toolchain for API developers, Postman是API开发人员的完整工具链,

Postman 邮差

or use Rest Client 或使用Rest Client

Advanced REST client 先进的REST客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM