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

json, php, post, rest

Solution

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.

Problem

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.

Original source