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));
Related
I use Unirest for PHP to do some HTTP Requests. Everything works fine, until I want to pass a fairly complex JSON to my Node.js router.
First I do a GET request that returns a JSON Object, then I extend this JSON object (needs to be done, I know it sucks) and want to feed it back into my other http POST request... and here is where the trouble starts:
I echoed the JSON that is returned and copied the output into postman -> works fine. If I want use the JSON directly in PHP in next request:
$teamsMemberOf is the variable which is containing the GET response.
$headers = array("Accept" => "application/json");
$newBody = '{"team":'.$teamsMemberOf->raw_body.'}';
$relevantBoxesAmount = Unirest\Request::post("http://localhost:3001/my/route/".$result['_id']."/get-something-from-server", $headers, $newBody);
and it doesn't work.
Error is 500 and 'Cannot read property '0' of undefined' which is certainly related to something in the JSON object.
Does someone have an idea how to fix it?
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"}
}
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
I am building an API using Codeigniter and Phils RESTserver and I am trying to send an array to the API using a POST request.
When I POST this:
query=("email#example.com","anna#nicole.com")
I can access it like this:
$this->post('query');
This produces:
("email#example.com","anna#nicole.com")
How can I loop through these email addresses in PHP?
Do anyone got another idea?
Thankful for all input!
If you are controlling the POST request, then you should POST an array to PHP, not a delimited list, which will automatically create an array that is available for use when the server receives the request. This is what J0HN means when he says use square brackets instead of parenthesis.
However, it depends on how you're sending the request to the server to determine the proper way to send an array to PHP. You might need:
query=["one#example.com", "two#example.com"]
query[]=["one#example.com", "two#example.com"]
Or other variants...
Since you don't elaborate on how you're making the request to the server, the proper way to POST an array cannot be determined.
On the other hand, if you can't POST an array for some reason, you can always send a JSON-encoded string and easily decode it:
// Assuming $this->post('query') = json_encode( array( "one#example.com", "two#example.com"));
$emails = json_decode( $this->post('query')); // Array of emails
OK, I am quite new to this Objective-C and JSON thing, so please bare with my questions. I am using Stig Json Framework and ASIHTTPRequest.
My overall objective is to send JSON data (containing login credential) to a PHP webservice and expecting a return once the PHP process the data. I need an authorize key from the return and also specific user data tied to the login credential.
Now, I can send the JSON data via POST all right, consume the data via PHP all right. But I just don't understand the logic of the return data, as I said earlier. I only know that ASIHTTPRequest do return the response string via anything echo'ed from the PHP, but is this the correct way to get the return data I want?
Or, do I encode it back to JSON? How?
I think another viable solution is to write a webservice that receive parameters via URL and return JSON data. For example, how do I write http://search.twitter.com/search.json?q=#<name>?
The data is returned as a JSON string and you need to convert it to Objective-C object using the "JSONValue" method before working with the returned data.
For example:
NSDictionary *results = [jsonString JSONValue];
Now you can work with the returned data as an NSDictionary.
You can json_encode data from PHP and echo that. Now you will receive JSON data in the ASIHTTPRequest delegate method, that is, a response string. Then use it as you like.
Remember to set the header type to json while you echo data from PHP.