The only way to send a JSON object is via JavaScript?
I'm developing a system that will have a lot of requests, send and receive Json.
If I can only work with Javascript to send the object, how do I trigger the js function?
Thanks in advance
[EDIT]
I have a C# working with database.
WCF making the communication between C# and PHP.
I was making the communication with WCF via SOAP, it was working fine but my boss want me to use JSON instead.
I've read some articles about JSON+PHP and the only way to send the JSON object is with JavaScript?
Once you have your JSON string encoded with json_encode() you simply echo it out with the correct MIME header.
header("Content-type: application/json");
echo json_encode($your_data);
to get the php file to return JSON you just make an array of the conditions and then json_encode echo it.....
response.php
$cond ? $response= array("conditions" => true) : $response = array("conditions"=>false);
echo json_encode($response);
Related
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 trying to implement a php client that sends an HTTP GET to the server which sends back a JSON object with the return information. I know how to decode the JSON once my php script has received it, but how would I go about actually getting it?
EDIT: Note - I send the server an HTTP GET, and it generates and sends back a JSON file. It is not a file sitting on the server.
Check out file_get_contents
$json = file_get_contents('http://somesite.com/getjson.php');
Browsers act differently based on what the server responds. It does not matter what type of request you make to the server (be it GET, POST, etc), but to return JSON as a response you have to set the header in the script you make the request to:
header('Content-Type: application/json;charset=utf-8;');
And then echo the JSON string, for example:
//...populating your result data array here...//
// Print out the JSON formatted data
echo json_encode($myData);
User agent will then get the JSON string. If AJAX made the request then you can simply parse that result into a JavaScript object that you can handle, like this:
//...AJAX request here...//
// Parse result to JavaScript object
var myData=JSON.parse(XMLHttp.responseText);
The header itself is not -really- necessary, but is sort-of good practice. JSON.parse() can parse the response regardless.
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'];
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.
From within PHP, how can I call an external JSON web service and then decode the returned string?
For example (pseudo-code):
<?php
$var jsonStr = json_decode("http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
?>
you almost had it!
<?php
$jsonObject = json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"));
?>
Nathan has it. You may want to explore the curl library for a more robust HTTP request approach, too.