Currently i have been working on a project which connects to a smsgateway. Once the user sends sms to gateway it redirects the requests to our server. Problem is they are received in $_GET method. But the sms provider says they are passing it in $_POST method. Url received at our end looks like the following.
http://www.example.com/smstest?msg=sample&id=55788
Is it possible to receive parameters in url when you use the $_POST method
An HTTP request can use the HTTP method POST and still use a URL that contains query parameters. POST is just a "verb" used in the HTTP header, the same as GET (and PUT and DELETE). A URL can always contain query parameters and it can also always contain a request body (though GET requests shouldn't). The PHP variable $_GET simply represents the parsed URL query parameters, the variable $_POST simply represents the parsed request body. They do not actually have anything to do with the HTTP verb and are therefore somewhat misnamed.
Yes, it is. The first line of every HTTP request contains the method (or verb) and the URI for which the request is made. No special restrictions are placed on the URI based on the choice of method, so POST requests may be made for a URI that includes a query string.
From PHP you can access the parameters in the query string normally through $_GET and $_REQUEST. Parameters passed as part of a submitted form are accessible as always through $_POST and $_REQUEST.
You can only have one verb (POST, GET, PUT, ...) when doing an HTTP Request. However, you can do
<form name="y" method="post" action"y.php?foo=bar">
and then PHP will populate $_GET['foo'] as well, although the Request was POST'ed.
For comment
By using
$_SERVER['REQUEST_METHOD']
Related
i can't use post with php on AWS Linux. i changed enable_post_data_reading=On and post_max_size=8M, but i still can't use post data. Where could it be deactivated ?
I checked the php code on an other server and there the post data are not null.
Have you accounted for this point noted in the PHP documentation for $_POST:
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
Does the POST request you are submitting specify the above Content-Type header values?
i found the reason, and it's simple, but maybe anybody have the same problem, the action url was wrong action="http://...." instead of https://
I'm having some issues with the PHP REST API of my app.
I'm submitting my parameters in the correct URL format: baseUrl?param1=val1¶m2=val2
However, the parameters are being filled into the $_GET array instead. I've looked into the $_SERVER array to see if there is something wrong there, but I can't see anything obvious.
$_SERVER['CONTENT_TYPE']="application/x-www-form-urlencoded"
$_SERVER['REQUEST_METHOD']="POST"
I've also checked my php.ini for post_max_size misconfig:
post_max_size=32M
I'm not able to figure what else could cause the parameters to be placed into $_GET
If it helps, I'm using XAMPP on Windows.
Also, using "Advanced REST client" for Chrome, I've noticed that regardless of the method, POST parameters are placed into $_GET if the parameters are in the URL, but if I move them into the REST client's Payload field, they show up as POST parameters.
You cannot get parameters via $_POST which are submitted in query-string. You can get them via $_GET array.
but if I move them into the REST client's Payload field, they show up as POST parameters., seeing your trouble, you can always get them with $_REQUEST
Just sharing a thought, The place from where you are initiating post request to backend, you have to use post method.
For example if you are using jquery
$.ajax ({ type: 'POST' })
If angular js
$http.post({ 'your code' });
I call my php file in the browser with "...test.php?text=Hello" but the $_POST variable stays empty (also print_r($_POST) returns array() ).
Why? Do I need to activate the post variable or something?
Thanks.
Variables passed in through the URL end up inside $_GET, not $_POST.
$_POST contains variables parsed by reading the HTTP request body when the method is POST. In this case the method is not POST and there is also no request body.
If you pass the variable through the URL you use $_GET.
Also, you will access the variable with:
$_GET['text']
This is a array that is sent through and you need to specify what item in the array you want to use.
...test.php?text=hello passes data via the GET method (accessible via $_GET in the processing script).
$_POST gets populated by forms or cURL access (when the transfer method is defined as "post")
How can I request a URL with my own variables without using it through a HTML form?
Thanks!
EDIT:
When a specific page loads I want to send a request to a url with a couple of my own variables.
Ex: I come to www.example.com/done
the page sends a request to www.example2.com?abc=123&def=456
Your question is too broad, but there is an answer...
By issuing HTTP request in a different way, for example:
by calling it using AJAX from some website (eg. jQuery.post()),
by issuing a request using some kind of tool, like browser extension (eg. Postman REST Client or Simple REST Client for Google Chrome),
by using some library to do such call in an automated way (eg. Requests library for Python),
In general, GET parameters are passed in the URL, while POST parameters are passed in the body, so to pass both, you need to do both, by issuing POST request and:
appending GET parameters to the URL, in URL-encoded way (like "...?par1=1&par2=2"),
passing POST parameters in the body of the request, also URL-encoded
POST variables are either sent through a form (hence, POSTing them), or sent using AJAX.
GETvariables, however, are simply passed through the URL. For instance, if you wanted to send foo=bar to example2.php, you could use
Go to example2.php
And then in example2.php, you could say
$foo = $_GET['foo'];
Simple. :)
Use AJAX in javascript. For a beginner I'd recommend using the jQuery library
$.get("url", {key: "value"}, function(resp) {})
or
$.post("url", {key: "value"}, function(resp) {})
Should suffice
What does post mean in the following?
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("POST", "url" + queryString, true);
because i'm not able to access variables using $_POST['var'] from url but
with $_REQUEST['var'] I can access value..
When you read from $_POST, you should pass your arguments in the HTTP body instead of using the querystring.
You would need to send your arguments as in the following example:
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("POST", "your_service.php", true);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send("var=100&another_var=200");
Your are not able to access the parameters via $_POST because you append them to the URL (i.e. they can be accessed via $_GET) and don't send them as POST data.
If you want to send the parameters via POST, have a look at the send() method.
POST is something included in an HTTP request (such as an XMLHTTPRequest).
In your case, you are adding the query string to the URL, which means that it is being passed as a GET variable. Even if it is a post request, PHP can still access any GET variables added on as a query string.
Based on your code, I don't think you are telling the request what info should be included in the POST section of the request, which would explain why you are not seeing anything with $_POST['var'].
But since $_REQUEST['var'] looks for request variables in GET and POST and any cookies passed in the request, you see the variable as it was passed via the query string.
Try echoing $_GET['var'] and you'll see that this is where the variable is getting the data from.
If you want to use POST the right way, you need to not point the request to a URL that has a query string and instead define that query string as the post data.
The post does mean the values are posted, but you should add them as post variables, while now you are only adding them to the url so you can only get them with $_REQUEST and $_GET.
Post data is usually passed in via the post data.
IIRC, you can pass it as an object via the send method.
ajaxRequest.send(requestString)