http request code
POST /parse.php HTTP/1.1
Host: mysite.com
Connection: Keep-Alive
Content-Length: 26
Content-Type: application/x-www-urlencoded
lt=12.123123&ln=123.123123
php code
//connect to database codes here
$database = "update name_tbl set lat='".$_GET['lt']."', lng='".$_GET['ln']."' where id=1";
mysqli_query($conn, $database)
So my problem I think is in the php part when I enter mysite.com/parse.php?lt=12.123123&ln=123.123123 to test if it's working and it does but when I the http request code on HttpRequest it but the send value is both 0.000000.
If you aren't sure whether the request has come to you via a GET or a POST method, you could try something like this
$lt = isset($_GET['lt']) ? $_GET['lt'] : (isset($_POST['lt']) ? $_POST['lt'] : null);
$ln = isset($_GET['ln']) ? $_GET['ln'] : (isset($_POST['ln']) ? $_POST['ln'] : null);
There might be another option called $_REQUEST which encompasses both $_GET and $_POST but has it own quirks too. Please have a read on the following link to get a better idea:
Among $_REQUEST, $_GET and $_POST which one is the fastest?
Related
Hope someone could spare time to help a rookie. I have to set up my server and then provide a supplier with the URL of the page which has to be able to receive a HTTP-Post request like the following - For each post received, your page will have to answer with a "+OK", in order to confirm the correct delivery of the notification.
POST /yourpage.php HTTP/1.1
Host: www.yoursite.com
Content-Length: 215
Connection: Keep-Alive
Content-type: application/x-www-form-urlencoded
Accept-Language: it
Cache-Control: no-cache
destination=%2B40757732753&text=sms+test+example&originator=%2B391234567890&date_time=20160606074445
What would be the best way to go about this instruction? I have some basic knowledge of PHP (still learning), so we can use PHP.
Thanks in advance
Marinda
You need learn about global variables in PHP, php has $_POST able to get content sended on body of post and do something with it.
<?php
// this will create a variable with data of destination sent
$destination = $_POST['destination']
...
// just print +OK
echo "+OK"
But if you want send SMS to Mobile, you need use services to send for you, in general has a cost for this and able to send a limited number of SMSs depend your plan.
I hope it help you
Check $_POST variable. It's a kind of special variable for this language. Then, you can:
<?php
$isOK = true;
// Check if POST parameter destination is set and it is not blank, you
// can repeat this validation with all your parameters, changing
// destination by its name.
if (!isset($_POST['destination']) || trim($_POST['destination']) == '') {
$isOK = false;
}
if ($isOK) {
echo "+OK";
}
I'm sending a POST request using WebClient.UploadData() method (C#) to my webserver. The packet sent to my webserver looks like so:
POST / HTTP/1.1
Host: {ip}
Content-Length: {length}
Expect: 100-continue
Connection: Keep-Alive
{buffer_content}
As the {buffer_content} is nowhere assigned in the $_POST array, I have the following question...
Question: How do I read the {buffer_content} with PHP?
I've stumbled upon file_get_contents('php://input'), but I'm unsure whether that is recommended to do.
Use the php://input stream:
$requestBody = file_get_contents('php://input');
This is the recommended way to do this and, in PHP 7.0, the only way. Previously, there was sometimes a global variable called $HTTP_RAW_POST_DATA, but whether it existed would depend on an INI setting, and creating it hurt performance. That variable was deprecated and removed.
Beware that prior to PHP 5.6, you can only read php://input once, so make sure you store it.
Once you have your body, you can then decode it from JSON or whatever, if you need that:
$requestBody = json_decode($requestBody) or die("Could not decode JSON");
I am developing the client side of a web application in iOS/Swift, and right now I am testing the part that communicates with the server. I setup a basic website on localhost at:
http://localhost/~username/ConnectivityTest/login
(which corresponds to /Users/username/Sites/ConnectivityTest/login on my Mac's file system).
The server side script (index.php on the directory above) is:
<?PHP
$userId = $_POST["userId"];
$password = $_POST["password"];
if (empty($userId) || empty($password)){
echo "Error: empty post variables";
}
else{
// Process credentials...
I am using the NSURLSession API on iOS, but I noticed that no matter how I configure my requests, even though the connection succeeds (i.e., returns an http code of 200 and the response body as data), the POST variables are unavailable (empty) on the server side.
So I decided to try sending the request manually using Postman on the browser (to try to rule out any mistakes on my iOS/Swift code), but I don't know how I should configure it (I am not versed in HTTP, it all is still a bit confusing to me):
Should I set the Content-Type header to application/json, application/x-www-form-urlencoded, or what?
Should I send the body data as form-data, x-www-form-urlencoded or raw?
In Postman, I set the body data (raw) as follows:
{
"userId":"my-user-name",
"password":"123456"
}
Alternativley, as form-data, it is:
userId my-user-name [Text]
password 12345 [Text]
As x-www-form-urlencoded, it is:
userId my-user-name
password 12345
Everything I try gives me the response "Error: empty post variables" that I set in my code's error path (i.e., $_POST['userId'] or $_POST['password'] are empty).
If, instead, I pass the variables as URL parameters:
http://localhost/~username/ConnectivityTest/login/index.php?userId=my-user-name&password=12345
...and access them in the script as &_GET['userId'] and $_GET['password'], it works fine.
what am I missing?
UPDATE: I created an HTML file in the same directory as the php file:
<html>
<body>
<form action="index.php" method="post">
User name: <input type="text" name="userId"><br>
Password: <input type="text" name="password"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
If I load the above page in a browser, fill in the fields and submit the form, the $_POST variables on my php script get the correct values. So the php code is correct, and I am setting up my request wrong in Postman (still don't know why).
UPDATE 2: Just in case there was a problem with localhost, I moved the script to a shared wb hosting service that I control, but the result is the same.
UPDATE 3: I must have missed it somehow before, but there is ONE setup that I got working:
Headers: Content-Type application/x-www-form-urlencoded
Body ("raw"): userId=my-user-name&password=123456
However, this restricts me to flat lists of key/value; If I wish to send more structured (i.e., nested) data to the server I need JSON support...
After searching here and there, I discovered that the post body data gets into the $_POST variables only when you send them as a form -i.e., application/x-www-form-urlencoded- (I guess that is what $_POST stands for, not the method used for the request). Correct me if I'm saying something that isn't correct.
When using a Content-Type of application/json, code like the following does the trick:
$data = json_decode(file_get_contents('php://input'), true);
$userId = $data["userId"];
$password = $data["password"];
I guess this is very basic stuff, but then again, my knowledge of HTTP is very limited...
A mistake I made when first using Postman was setting the params when using the POST method which would fail. I tried your Update 3 which worked and then I realized there were key value pairs in the Body tab.
Removing the params, setting the Body to "x-www-form-urlencoded" and adding the variables to be posted here works as expected.
My oversight was I figured there would be a single section to enter the values and the method would determine how to pass them along which makes sense in case you would like to send some parameters in the URL with the POST
Comment by #FirstOne saved me. I added a forward slash to the URL and it solved the problem. This way, my php script could detect the request as POST even without setting header to
Content-Type application/x-www-form-
urlencoded
I also tested my code without a header and it works fine. I tested with Content-type: application/json too and it works fine.
if($_SERVER['REQUEST_METHOD] = 'POST') { echo 'Request is post'; }
My script returned 'Request is post' using RESTEasy - Rest client on Chrome.
Thanks, FirstOne.
So I'm working on a group project for school, and we're working with a client who wants a companion app of sorts to go with his companies device. We provide the device a host IP or domain and it sends HTTP Post requests in the form of XML every 5 seconds or so. The problem we're having is we have NO idea how to capture the data being sent on our server. Simply trying to grab and dump all $_POST data yields an empty array, and our attempts to use a socket have produced similar results.
We've tried pointing the device to http://posttestserver.com/ - and it gets the data perfectly, though there is no source code available to see how the site operates. Admittedly our knowledge of server side scripting is limited at best as we've only been working with PHP for a couple months, and this isn't something that has been covered.
The above mentioned post server produces the following output ( with some omitted data for privacy ). Any help in reproducing this or simply assistance in getting the data on our server would be greatly appreciated!
Time: Sun, 09 Nov 14 14:20:26 -0800
Source ip: ######
Headers (Some may be inserted by server)
HTTP_CONNECTION = close
REQUEST_URI = /post.php
QUERY_STRING =
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = ######
REMOTE_ADDR = ######
CONTENT_LENGTH = 488
CONTENT_TYPE = application/xml
HTTP_USER_AGENT = Raven Uploader/v1
HTTP_FROM = ######
HTTP_ACCEPT = */*
HTTP_HOST = posttestserver.com
HTTPS = on
UNIQUE_ID = VF-oqtBx6hIAACKZ7j0AAAAH
REQUEST_TIME_FLOAT = 1415571626.7993
REQUEST_TIME = 1415571626
No Post Params.
== Begin post body ==
<?xml version="1.0"?><clientcompany macId="######" version="1.1" timestamp="1415571625s">
<PriceCluster>
<DeviceMacId>######</DeviceMacId>
<MeterMacId>######</MeterMacId>
<TimeStamp>0x1bf2a52d</TimeStamp>
<Price>0x00000467</Price>
<Currency>0x007c</Currency>
<TrailingDigits>0x04</TrailingDigits>
<Tier>0x01</Tier>
<StartTime>0x1bf2a52d</StartTime>
<Duration>0xffff</Duration>
<RateLabel>Block 2</RateLabel>
</PriceCluster>
</clientcompany>
== End post body ==
Upload contains PUT data:
<?xml version="1.0"?><clientcompany macId="0xd8d5b90016d1" version="1.1" timestamp="1415571625s">
<PriceCluster>
<DeviceMacId>######</DeviceMacId>
<MeterMacId>######</MeterMacId>
<TimeStamp>0x1bf2a52d</TimeStamp>
<Price>0x00000467</Price>
<Currency>0x007c</Currency>
<TrailingDigits>0x04</TrailingDigits>
<Tier>0x01</Tier>
<StartTime>0x1bf2a52d</StartTime>
<Duration>0xffff</Duration>
<RateLabel>Block 2</RateLabel>
</PriceCluster>
</clientcompany>
You need to capture the raw input stream:
//$data = $_POST; <-- will be empty unless you are sending a key value pair(s)
$data = file_get_contents('php://input'); //<-- will capture all posted data
echo '== Begin post body ==';
echo $data;
echo '== End post body ==';
If you need to see headers as well you can use getallheaders function: http://php.net/manual/en/function.getallheaders.php
instead of $_POST check $HTTP_RAW_POST_DATA var
I'm trying to edit and tweak someone else's REST server in PHP. It's based on the REST Server written by Phil Sturgeon. Pretty much got my head around all of it, but my requests aren't working as expected.
In the server constructor is the code
switch ($this->request->method)
{
case 'post':
$this->_post_args = $_POST;
$this->request->format and $this->request->body =
file_get_contents('php://input');
break;
}
I know that php://input can only be read once, so doing var_dump(file_get_contents('php://input')) before setting the variables shows that my XML data is being read correctly from the input stream but obviously the variables aren't set right.
But doing var_dump($this->request->body) only outputs NULL! Is there a special technique to storing the contents of php://input in a variable?
EDIT:
I'm using API Kitchen to send the POST request and the headers that it sends are
Status: 200
X-Powered-By: PHP/5.3.2-1ubuntu4.11
Server: Apache/2.2.14 (Ubuntu)
Content-Type: application/xml
Date: Fri, 10 Feb 2012 11:00:43 GMT
Keep-Alive: timeout=15, max=100
Content-Length: 936
Connection: Keep-Alive
I can't see from this what the encoding is.
EDIT 3:
The encoding is application/x-www-form-urlencoded which could be where the problem lies!! How do I specifically say what this should be?
EDIT 2:
$this->request->method contains 'post'
Thanks for all the help, it turns out that in order to work, the content type of the request must be application/xml, not application/x-www-form-urlencoded as it was.
if $this->request->format evaluates to false or NULL or 0, the later part of and operator does not execute.
$this->request->format and $this->request->body = file_get_contents('php://input');
^
|
+--- this part wont execute
You should have written it like
if($this->request->format){
$this->request->body = file_get_contents('php://input');
}
This helps in debugging.