This question already has answers here:
Reading JSON POST using PHP
(3 answers)
Closed 7 years ago.
I want to send json payload to my PHP webservice. I am testing on advance rest client. I already search similar answers. But it is not helping me.
I am sending this json to my php service which is running on localhost.
{
"name":"XXXX",
"password":"YYYYY"
}
I changed the content-type from dropdown to application/json.
However sending value as form parameter from advance rest client works fine.But this is not what i want.
My php code is
$name= isset($_POST['name']) ? mysql_real_escape_string($_POST['name']) : "";
$pass= isset($_POST['pass']) ? mysql_real_escape_string($_POST['pass']) : "";
echo $name;
echo $pass;
but it is always printing the blank value.
I am new to php but found this is the way to receive post params.
I found the solution if any one else facing same issue. To read the json data sent in the request body we can use this method
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);
$name = isset($data->name) ? mysql_real_escape_string($data->name) : "";
echo $name
}
Now it will give the correct value.
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
I am trying to send a basic confirmation email after a user submits my html form using the php mail() function, but for some reason the email won't send. Here is my code:
<?php
echo "You're all set! You should be recieving a confirmation email within the next few minutes.";
$to = $_POST["email"];
$subject = "Your Appointment";
$msg = "Your appointment has been scheduled for " . $_POST["ApptDate"] . ".";
mail($to,$subject,$msg);
?>
I know my form is working correctly, because I am taken to this page after I submit the form. Also, "email" and "ApptDate" are keys I stored in my html form, and when I echo the variables they all print, so I don't think its a variable related error. Any help would be greatly appreciated.
is it localhost? or a server?
You would need an SMTP server setup in either case,
For localhost i use this one as its a one-click-never-touch-again setup
https://toolheap.com/test-mail-server-tool/users-manual.html
Im working with so called webhooks. What basically happens is. There's a process happening in the background and when that process finishes it will send a POST request to an URL that I have to specify. For example 'www.bla/process.php'.
The post request that is sent will have a body of data. My question is , is it possible to read the data that is sent and just print it out for example?
Yes
It is possible to pass info from one page to another and print it out for example.
There are many method's...
// THE SAME
echo $_POST['DATA1'];
echo ($_POST['DATA1']);
// THE SAME
// IF = DATA IS SET AND NOT EMPTY
if (isset($_POST['DATA1']) && !empty($_POST['DATA1'])) {
$DATA1 = $_POST['DATA1'];
}
echo($DATA1);
This question already has answers here:
Manually parse raw multipart/form-data data with PHP
(8 answers)
Closed 3 years ago.
I am using a .php file to handle an HTTP delete request, and I would like to access the data sent with the delete request. I know the standard way to do this right now is:
parse_str(file_get_contents('php://input'),$_DELETE);
Then one can access the data via $_DELETE['dataName']. This is not working for me however, here is my exact code below:
else if($_SERVER['REQUEST_METHOD'] == "DELETE"){
parse_str(file_get_contents('php://input'),$_DELETE);
echo "contents of global DELETE variable:\n";
echo var_dump($_DELETE);
echo "contents of file get contents method:\n";
echo file_get_contents('php://input');
}
This is what is printed out:
contents of global DELETE variable: array(1) { ["------------------------
----454596603246993785859143 Content-Disposition:_form-data;_name"]=>
string(67) ""id" 5 ----------------------------454596603246993785859143--
" }
contents of file get contents method: ---------------------------
-454596603246993785859143 Content-Disposition: form-data; name="id" 5 ---
-------------------------454596603246993785859143--
In this example is used "postman" to send a delete request to my php server with a generic id set to 5 as data. Instead of getting an associative array with "id=>5", I get a bunch of random data stored in an indexed array. Any help would be greatly appreciated as I have spent far too long trying to solve this issue.
I have tried this following way & it works perfectly in the Postman so you can try this,
else if($_SERVER['REQUEST_METHOD'] == "DELETE"){
$output = json_decode(file_get_contents('php://input'), true);
$id = $output['id'];
echo $id;
}
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
For some reason My PHP email form does not send the html form data, the headings are there, but the imputed data is not however when the files are put on the development server, a sub domain of the main website 123.123.com, the email sends with the form data, with no issue at all.
It seems to do this on all the forms on the website, could this be server related? or am I missing something. any help would be appreciated.
<?php
mail( "Test#test.con, "Interlinks Sign Up",
"\nName: - $fulllname
\nEmail:- $email
\nContact Number: - $contactnumber
\nCountry:- $country
\nTown/City:- $city
\nPost Code:- $postcode
\nAbout Yourself: - $about
\nWork And Business: - $work
\nJob Description: - $job
\nIndustry Description: - $industry
\nWebsite: - $website
\nLanguage: - $language
\nWho To Meet:- $meet
\nAgree: - $agree ",
"From: blank#test.com" );
?>
In order to get the data from your HTML form you will need to use $_POST['form_element_name_here'] or $_GET['form_element_name_here'] depending on the type of form action that you used. If you are already doing thing and are binding these to the variables that you are using in your original code we will need to have a look at the entire PHP script and HTML front end that is processing the form.
I'm currently trying to setup a page which receives XML via an HTTP POST. I have successfully used SimpleXML to retrieve the XML from a file and then perform my logic, but I am unsure how to set it up to receive a POST submission.
Is there a default way to retrieve all information from $_POST as a string?
//'get'ting the xml from a file
$job = simplexml_load_file(/path/to/file);
//my assumption on how to accept the XML post - throws a not string error
$job = simplexml_load_string($_POST);
As the is being received from a third party, is there extra information that I am not being supplied? All my previous handlings have been with name=value pairs, i.e. $value = $_POST['name']; To rephrase, do all HTTP POSTs have a name handle to them?
Sorry for the multi-faceted question, I'm a bit lost, so am trying to cover all angles.
Any help is greatly appreciated!
You're most likely looking for the raw POST data.
$postdata = file_get_contents("php://input");
This code will combine all posted variables into a single string variable:
$foo = "";
foreach( $_POST as $val )
{
$foo .= $val;
}
well, if you are receiving xml with using post, why not are you using xmlrpc?