Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a Restful webservice with POST requests in PHP without using any library?

Tags:

json

rest

post

php

Basic requirement is to know if we can iterate through $_POST when the request body payload has data sent in json format e.g.

{"City":{"countryCode": "IN","regionCode":"KR"}}

We are able to access this only if we send the data as

m={"City":{"countryCode": "IN","regionCode":"KR"}}

We are able to access this using $_POST['m'] The Content-Type is set to default application/x-www-form-urlencoded, when we set this as application/json $_POST is empty/null.

If we try to access this as $_POST instead of $_POST['m'] it returns null/empty.

NB: I am newbie to PHP. Is it possible to create webservices without any library. Without making use of any library can PHP accept the post request with json data.

like image 639
Venu M Avatar asked Nov 29 '25 20:11

Venu M


2 Answers

To get raw POST data (as opposed to having to access individual POST variables such as $_POST['m']), you can use the following wrapper:

$json = file_get_contents('php://input');

You can read the manual on wrappers if you're interested in learning a bit more about them.

like image 82
Wayne Whitty Avatar answered Dec 02 '25 09:12

Wayne Whitty


This is how i am getting data and saving it to my database.

 $return    = array();
 $data      = json_decode(stripslashes($_REQUEST['Data']));

 $email     = $data->{"paramA"};
 $password      = $data->{"paramB"};

i think it might help you.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!