This question may be stupid. I want to know if its possible to send a string like {"type":"kissme", "who":"john bennet"} to rest controller as a post request.
I know that post data has to be sent as somekey=somevalue&otherkey=othervalue as body of the request. While receiving we refer to the key to get the data. But how can we send any string without assigning to a key like json data ? If it is possible to send, how to receive ?
Thank you,
Abbiya
REST implementation for CodeIgniter
You can use any of the following formats too simply by appending
/format/json to the end of your URL or passing the correct MIME-type:
xml
json
serialize
php (raw output that can be used in eval)
html
csv
Source: http://philsturgeon.co.uk/blog/2009/06/REST-implementation-for-CodeIgniter
i used file_get_contents('php://input'); while reading in controller. i use the content-type header as application/x-www-form-urlencoded while posting the data
Related
I have to write a RESTfull service in PHP which can send json data to the caller and retrieve json (or xml) data from the user. I know how to send json or xml data, but not how I can get data back from the user.
The simplest way is getting JSON data as POST or PUT body. To get PHP body:
$entityBody = file_get_contents('php://input');
and then decode json into a PHP object:
$requestBody = json_decode ( $entityBody);
If you are not sour the request body is XML or JSON then check Content-Type in the header.
A RESTful api usually doesnt expect a "response" from the client.
What you want to do is to create an endpoint for the client to POST the specific content.
/api/user/?json={...}
On the PHP side you can retrieve the data with $_REQUEST['json'] (which includes both POST and GET.
There is something called HATEOS that can be used for telling the client about links associated with the current resource (if you want to "chain" calls between the client and the service).
I search SO for similar questions, but even though the title was same, I was unable to find an answer so I am reposting.
I have a REST endpoint which till now was a POST call even though it was only retrieving data. I am trying to convert this to a GET call. The payload is a JSON for the POST method. I know I can convert the JSON into URL parameters and pass it. However I wanted to check if I can pass this JSON as a GET request itself.
Is there any way in PHP to pass JSON in a GET request?
It's a messy proposition but you should be able to urlencode your JSON and pass it that way
$data = ['some' => 'value'];
echo urlencode(json_encode($data));
I'm setting an API for my server for another developer. I'm currently using Flash AIR to send POST data to my server, and simply extract the variables as in
$command = $_POST['command'].
However, he's not using Flash, and is sending data like so:
https://www.mysite.com POST /api/account.php?command=login HTTP/1.1
Content-Type: application/json
Connection: close
command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
My server is returning him an error saying that the 'command' parameter is missing.
What do I need to do my end to extract the $_POST var 'command' from his above data?
I've tried file_get_contents('php://input') and http_get_request_body(), but although they don't error, they don't show anything.
Thanks for your help.
The request claims that it is sending JSON.
Content-Type: application/json
However, this:
command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
… is not JSON.
If you get rid of everything before the { then it would be JSON and you should be able to read it with file_get_contents('php://input') (and could then pass it through a decoder.
I've tried file_get_contents('php://input') and http_get_request_body() … they don't show anything.
They should work.
When I print out file_get_contents('php://input') for the comms … I get command=login, yet...
I thought you said you didn't get anything
if(!isset($_POST['command']))
$_POST will only be populated for the two standard HTML form encoding methods. If you are using JSON then it won't be automatically parsed, you have to do it yourself (with valid JSON input (so the additional data would need to be encoded in the JSON text with the rest of the data)), file_get_contents('php://input') and decode_json).
"Content-Type should be www-form-urlencoded" from #Cole (correct answer)
More info here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
The command parameter needs to be part of the data, and the whole thing should be valid JSON. As is, command=login, it is not valid JSON.
Add it to the params object or make a containing object, like
{
command:'login',
params :{"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
}
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.
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.
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.
I am building an API using Codeigniter and Phils RESTServer.
I want to send a bounch of email addresses to the API (best in JSON format).
How can I do this? How can I "receive" and "use" the JSON object that is sent to the server?
Right now I can send and receive parameters like this:
email=example#example.com
and I receive and use them like this:
$this->post('email')
I want to send and receive in this format instead
{"email":"example#example.com"}
How can I achieve this and how can I use the object?
UPDATE: I at least need to be able to send a normal array to the RESTserver.
Thankful for all input!
There is an output class in codeigniter. For json encode, you can use this:
$contents = $this->output
->set_content_type('application/json')
->set_output(json_encode(array('email' => 'example#example.com')));
echo $contents;//{"email":"example#example.com"}
How about posting the JSON string to the controller and then using json_decode() to extract the data out if it?
$data = json_decode($this->post('json'));
echo $data['email'];