通过URL中的post方法获取值

时间:2022-04-03 13:36:31

I am trying some code to get value from URL through post method and search database table for that value and get info from the database and encode it into JSON response.

我正在尝试一些代码从URL通过post方法和搜索数据库表获取该值的值,并从数据库获取信息并将其编码为JSON响应。

Here is my code :

这是我的代码:

<?php
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","json") or die("Error " . mysqli_error($connection));
if (isset($_POST['empid'])) {
    $k = $_POST['empid'];
    //fetch table rows from mysql db
    $sql = "select `salary` from tbl_employee where `employee_id` = $k ";
} else {
    //fetch table rows from mysql db
    $sql = "select `salary` from tbl_employee";
}
//fetch table rows from mysql db
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
echo json_encode($emparray);

//close the db connection
mysqli_close($connection);
?>

I used Postman extension on Chrome and pass the values but it is not returning. Instead it is returning the else part.

我在Chrome上使用Postman扩展程序并传递值,但它没有返回。而是返回其他部分。

Postman Screenshot 通过URL中的post方法获取值

11 个解决方案

#1


2  

As per your screenshot you are sending your empid through query parameter so you need to access that as follows

根据您的屏幕截图,您将通过查询参数发送您的empid,因此您需要按如下方式访问它

<?php
if (isset($_GET['empid'])) {
    echo $_GET['empid'];
}else{
    // else part
}

also for that you need to Request Url in Postman using GET method.

同样,你需要使用GET方法在Postman中请求Url。

But as you have stated that you want to send empid through POST in postman, you have to send it through form-data in Postman and access it as $_POST["empid"];. following is the screenshot for your reference 通过URL中的post方法获取值

但正如你已经声明要在postman中通过POST发送empid,你必须通过Postman中的表单数据发送它并将其作为$ _POST [“empid”];进行访问。以下是截图供您参考

else there is another option where you can send the POST data through body as row json and access it as

否则,还有另一个选项,您可以通过正文将POST数据作为行json发送并将其作为

$rawPostData = file_get_contents('php://input');
$jsonData = json_decode($rawPostData);

and $post will contain the raw data. And you can send it through postman as in following screenshot.

和$ post将包含原始数据。您可以通过邮递员发送,如下面的屏幕截图所示。

通过URL中的post方法获取值

#2


3  

Looking at your screen shot, you have not passed body key values, instead you passed params.

查看屏幕截图,您没有传递正文键值,而是通过了params。

通过URL中的post方法获取值 Click on Body Tab and then pass key & value pair.

单击Body选项卡,然后传递键和值对。

#3


1  

You have to set the Body to "x-www-form-urlencoded" and adding the variables to be posted

您必须将Body设置为“x-www-form-urlencoded”并添加要发布的变量

通过URL中的post方法获取值

Or try this SO question, its already been answered

或者尝试这个问题,它已经得到了解答

#4


1  

I replicated the code and db on my system to figure out the problem. I also added some lines of code before if (isset($_POST['empid'])) { for diagnostics sake:

我在我的系统上复制了代码和数据库,以找出问题所在。我之前添加了一些代码行if(isset($ _ POST ['empid'])){用于诊断:

$method = $_SERVER['REQUEST_METHOD'];
echo $method."<br/>";

The application file is index.php deployed in json directory inside webroot.

应用程序文件是在webroot内部的json目录中部署的index.php。

When I send any request to http://localhost/json directory (either POST/GET), Apache redirects the request as a GET request to index.php (as configured in my Apache config file). I assume this is what you're experiencing.

当我向http:// localhost / json目录(POST / GET)发送任何请求时,Apache会将请求重定向为对index.php的GET请求(在我的Apache配置文件中配置)。我认为这就是你所经历的。

But when I send the request to http://localhost/json/index.php, the request is accurately received and processed.

但是当我将请求发送到http://localhost/json/index.php时,会准确地接收和处理请求。

Therefore, I would say the solution is that you need to specify the php file and also set the empid parameter as part of the body in Postman (not as part of the url).

因此,我想说解决方案是你需要指定php文件,并将empid参数设置为Postman中的一部分(不作为url的一部分)。

通过URL中的post方法获取值

通过URL中的post方法获取值

#5


0  

I think you should also check the post if emptyif (isset($_POST['empid']) AND ($_POST['empid']) != ""). to allow php to execute the line before else.Sometimes programming becomes unpredictable.

我想你也应该检查帖子是否为emptyif(isset($ _ POST ['empid'])AND($ _POST ['empid'])!=“”)。允许php在else之前执行该行。有时编程变得不可预测。

#6


0  

use if(isset($_REQUEST['empid'])) to test in POSTMAN...

使用if(isset($ _ REQUEST ['empid']))在POSTMAN中测试...

Then use if(isset($_POST['empid'])) to test directly from app...

然后使用if(isset($ _ POST ['empid']))直接从app测试...

have a look Issue in POSTMAN https://github.com/postmanlabs/postman-app-support/issues/391

看看POSTMAN中的问题https://github.com/postmanlabs/postman-app-support/issues/391

#7


0  

To get the value of a variable from the URL(query string), you need to use either $_GET or $_REQUEST.$_POST represents data that is sent to the script via the HTTP POST method.

要从URL(查询字符串)获取变量的值,您需要使用$ _GET或$ _REQUEST。$ _ POST表示通过HTTP POST方法发送到脚本的数据。

So, in your code you just need to do this :

所以,在你的代码中你只需要这样做:

$_REQUEST['empid'] instead of $_POST['empid']

$ _REQUEST ['empid']而不是$ _POST ['empid']

In POST method the data is sent to the server as a package in a separate communication with the processing script. Data sent through POST method will not visible in the URL.

在POST方法中,数据作为包在与处理脚本的单独通信中发送到服务器。通过POST方法发送的数据在URL中不可见。

Confirm that in postman Content-Type should be application/x-www-form-urlencoded in request header.

确认邮递员内容类型应在请求标头中为application / x-www-form-urlencoded。

Postman reference doc : https://www.getpostman.com/docs/requests

邮差参考文档:https://www.getpostman.com/docs/requests

#8


0  

Hey it sounds like you are just needing to do a GET request to your DB.

嘿,听起来你只需要向你的数据库发出GET请求。

You are more than welcome to send in variables via a GET request as well.

我们非常欢迎您通过GET请求发送变量。

GET http://localhost/json?empid=3

You can then get data from your GET request like so $_GET['empid']

然后,您可以从GET请求中获取数据,如$ _GET ['empid']

I suggest a GET request because I see your not actually posting any data to your server, your just handing in a variable in which you want to use to query with.

我建议一个GET请求,因为我看到你实际上没有向你的服务器发布任何数据,你只需要输入一个你想用来查询的变量。

I do understand that GET requests are less secure, but in your scenario your POST just doesn't seem to want to work. So a different tack might do you justice.

我确实理解GET请求不太安全,但在您的场景中,您的POST似乎并不想工作。所以一个不同的方法可能会让你公正。

#9


0  

If you want a value from the URL, you need to use $_GET["empid"] instead $_POST["empid"]

如果你想要一个URL的值,你需要使用$ _GET [“empid”]而不是$ _POST [“empid”]

#10


0  

Looking at the URL you are requesting, you are sending a GET value within your POST request.

查看您请求的URL,您将在POST请求中发送GET值。

http://localhost/json?empid=3

As you can see here, the url holds the empid variable and so the is send to the server as beeing a GET variable ($_GET)

正如你在这里看到的那样,url保存了empid变量,所以它被发送到服务器作为一个GET变量($ _GET)

Use $_GET['empid'] to access this variable, while using $_POST to access the other variables.

使用$ _GET ['empid']访问此变量,同时使用$ _POST访问其他变量。

You could also use $_REQUEST to access both GET and POST data by the same global.

您还可以使用$ _REQUEST通过相同的全局访问GET和POST数据。

#11


0  

Submitting a form through POST method

通过POST方法提交表单

By post method of form submission, we can send number or length of data. Sensitive information like password does not get exposed in URL by POST method, so our login forms we should use POST method to submit data. This is how we collect data submitted by POST method in PHP

通过表格提交的邮寄方法,能发送数据的数量或者长度。密码等敏感信息不会通过POST方法暴露在URL中,因此我们的登录表单应该使用POST方法提交数据。这就是我们如何收集PHP中POST方法提交的数据

$id=$_POST['id'];

$password=$_POST['password'];

Collecting data submitted by either GET or POST method

收集GET或POST方法提交的数据

If a page is receiving a data which can come in any one of the method GET or POST then how to collect it ? Here we are not sure how to collect the data. So we will use like this.

如果页面正在接收可以采用GET或POST方法中的任何一种方式的数据,那么如何收集它?在这里,我们不确定如何收集数据。所以我们会这样使用。

$id=$_REQUEST['id'];

$password=$_REQUEST['password'];

#1


2  

As per your screenshot you are sending your empid through query parameter so you need to access that as follows

根据您的屏幕截图,您将通过查询参数发送您的empid,因此您需要按如下方式访问它

<?php
if (isset($_GET['empid'])) {
    echo $_GET['empid'];
}else{
    // else part
}

also for that you need to Request Url in Postman using GET method.

同样,你需要使用GET方法在Postman中请求Url。

But as you have stated that you want to send empid through POST in postman, you have to send it through form-data in Postman and access it as $_POST["empid"];. following is the screenshot for your reference 通过URL中的post方法获取值

但正如你已经声明要在postman中通过POST发送empid,你必须通过Postman中的表单数据发送它并将其作为$ _POST [“empid”];进行访问。以下是截图供您参考

else there is another option where you can send the POST data through body as row json and access it as

否则,还有另一个选项,您可以通过正文将POST数据作为行json发送并将其作为

$rawPostData = file_get_contents('php://input');
$jsonData = json_decode($rawPostData);

and $post will contain the raw data. And you can send it through postman as in following screenshot.

和$ post将包含原始数据。您可以通过邮递员发送,如下面的屏幕截图所示。

通过URL中的post方法获取值

#2


3  

Looking at your screen shot, you have not passed body key values, instead you passed params.

查看屏幕截图,您没有传递正文键值,而是通过了params。

通过URL中的post方法获取值 Click on Body Tab and then pass key & value pair.

单击Body选项卡,然后传递键和值对。

#3


1  

You have to set the Body to "x-www-form-urlencoded" and adding the variables to be posted

您必须将Body设置为“x-www-form-urlencoded”并添加要发布的变量

通过URL中的post方法获取值

Or try this SO question, its already been answered

或者尝试这个问题,它已经得到了解答

#4


1  

I replicated the code and db on my system to figure out the problem. I also added some lines of code before if (isset($_POST['empid'])) { for diagnostics sake:

我在我的系统上复制了代码和数据库,以找出问题所在。我之前添加了一些代码行if(isset($ _ POST ['empid'])){用于诊断:

$method = $_SERVER['REQUEST_METHOD'];
echo $method."<br/>";

The application file is index.php deployed in json directory inside webroot.

应用程序文件是在webroot内部的json目录中部署的index.php。

When I send any request to http://localhost/json directory (either POST/GET), Apache redirects the request as a GET request to index.php (as configured in my Apache config file). I assume this is what you're experiencing.

当我向http:// localhost / json目录(POST / GET)发送任何请求时,Apache会将请求重定向为对index.php的GET请求(在我的Apache配置文件中配置)。我认为这就是你所经历的。

But when I send the request to http://localhost/json/index.php, the request is accurately received and processed.

但是当我将请求发送到http://localhost/json/index.php时,会准确地接收和处理请求。

Therefore, I would say the solution is that you need to specify the php file and also set the empid parameter as part of the body in Postman (not as part of the url).

因此,我想说解决方案是你需要指定php文件,并将empid参数设置为Postman中的一部分(不作为url的一部分)。

通过URL中的post方法获取值

通过URL中的post方法获取值

#5


0  

I think you should also check the post if emptyif (isset($_POST['empid']) AND ($_POST['empid']) != ""). to allow php to execute the line before else.Sometimes programming becomes unpredictable.

我想你也应该检查帖子是否为emptyif(isset($ _ POST ['empid'])AND($ _POST ['empid'])!=“”)。允许php在else之前执行该行。有时编程变得不可预测。

#6


0  

use if(isset($_REQUEST['empid'])) to test in POSTMAN...

使用if(isset($ _ REQUEST ['empid']))在POSTMAN中测试...

Then use if(isset($_POST['empid'])) to test directly from app...

然后使用if(isset($ _ POST ['empid']))直接从app测试...

have a look Issue in POSTMAN https://github.com/postmanlabs/postman-app-support/issues/391

看看POSTMAN中的问题https://github.com/postmanlabs/postman-app-support/issues/391

#7


0  

To get the value of a variable from the URL(query string), you need to use either $_GET or $_REQUEST.$_POST represents data that is sent to the script via the HTTP POST method.

要从URL(查询字符串)获取变量的值,您需要使用$ _GET或$ _REQUEST。$ _ POST表示通过HTTP POST方法发送到脚本的数据。

So, in your code you just need to do this :

所以,在你的代码中你只需要这样做:

$_REQUEST['empid'] instead of $_POST['empid']

$ _REQUEST ['empid']而不是$ _POST ['empid']

In POST method the data is sent to the server as a package in a separate communication with the processing script. Data sent through POST method will not visible in the URL.

在POST方法中,数据作为包在与处理脚本的单独通信中发送到服务器。通过POST方法发送的数据在URL中不可见。

Confirm that in postman Content-Type should be application/x-www-form-urlencoded in request header.

确认邮递员内容类型应在请求标头中为application / x-www-form-urlencoded。

Postman reference doc : https://www.getpostman.com/docs/requests

邮差参考文档:https://www.getpostman.com/docs/requests

#8


0  

Hey it sounds like you are just needing to do a GET request to your DB.

嘿,听起来你只需要向你的数据库发出GET请求。

You are more than welcome to send in variables via a GET request as well.

我们非常欢迎您通过GET请求发送变量。

GET http://localhost/json?empid=3

You can then get data from your GET request like so $_GET['empid']

然后,您可以从GET请求中获取数据,如$ _GET ['empid']

I suggest a GET request because I see your not actually posting any data to your server, your just handing in a variable in which you want to use to query with.

我建议一个GET请求,因为我看到你实际上没有向你的服务器发布任何数据,你只需要输入一个你想用来查询的变量。

I do understand that GET requests are less secure, but in your scenario your POST just doesn't seem to want to work. So a different tack might do you justice.

我确实理解GET请求不太安全,但在您的场景中,您的POST似乎并不想工作。所以一个不同的方法可能会让你公正。

#9


0  

If you want a value from the URL, you need to use $_GET["empid"] instead $_POST["empid"]

如果你想要一个URL的值,你需要使用$ _GET [“empid”]而不是$ _POST [“empid”]

#10


0  

Looking at the URL you are requesting, you are sending a GET value within your POST request.

查看您请求的URL,您将在POST请求中发送GET值。

http://localhost/json?empid=3

As you can see here, the url holds the empid variable and so the is send to the server as beeing a GET variable ($_GET)

正如你在这里看到的那样,url保存了empid变量,所以它被发送到服务器作为一个GET变量($ _GET)

Use $_GET['empid'] to access this variable, while using $_POST to access the other variables.

使用$ _GET ['empid']访问此变量,同时使用$ _POST访问其他变量。

You could also use $_REQUEST to access both GET and POST data by the same global.

您还可以使用$ _REQUEST通过相同的全局访问GET和POST数据。

#11


0  

Submitting a form through POST method

通过POST方法提交表单

By post method of form submission, we can send number or length of data. Sensitive information like password does not get exposed in URL by POST method, so our login forms we should use POST method to submit data. This is how we collect data submitted by POST method in PHP

通过表格提交的邮寄方法,能发送数据的数量或者长度。密码等敏感信息不会通过POST方法暴露在URL中,因此我们的登录表单应该使用POST方法提交数据。这就是我们如何收集PHP中POST方法提交的数据

$id=$_POST['id'];

$password=$_POST['password'];

Collecting data submitted by either GET or POST method

收集GET或POST方法提交的数据

If a page is receiving a data which can come in any one of the method GET or POST then how to collect it ? Here we are not sure how to collect the data. So we will use like this.

如果页面正在接收可以采用GET或POST方法中的任何一种方式的数据,那么如何收集它?在这里,我们不确定如何收集数据。所以我们会这样使用。

$id=$_REQUEST['id'];

$password=$_REQUEST['password'];