Welcome Guest, Not a member yet? Register   Sign In
CI Client Parsing JSON
#1

(This post was last modified: 03-30-2015, 12:14 PM by cartalot.)

Editing this question to make it clearer

Server A  
sends a JSON encoded array with the proper json http headers to ->

Server B  -
receives the JSON, does some db functions, returns an http status code to -> Server A

how does server B capture the JSON? is it capturing the HTTP body?
how do you tell codeigniter or PHP -- take the entire body of this message and convert it from JSON?
is there a message body function or something similar?
Reply
#2

(This post was last modified: 03-30-2015, 04:39 AM by RWCH.)

Can you tell what specific problem you have? Returning JSON is easy.

An example:

PHP Code:
 $tennisArray = array('Djokovic' => 1'Federer' => 2'Nadal' => 3'Murray' => 4);

 echo json_encode($tennisArray); //Take care you only echo JSON objects 

The code above will output the JSON formatted data like this:

Code:
{"Djokovic":1,"Federer":2,"Nadal":3,"Murray":4}

And if you need to encode an object, the following will probably do:

PHP Code:
 echo json_encode((array)$object); 

Good luck
Reply
#3

thanks for response, my original question was confusing so i edited it above, hopefully i've made it clearer.
Reply
#4

Your best bet is to have server A send a POST to Server B with the JSON string included in a field.

Server A sends a POST request to Server B with a field of say,
PHP Code:
$payload json_encode($myData); 

Then on Server B at your endpoint, you grab it using either
PHP Code:
$_POST["payload"
or
PHP Code:
$this->input->("post"); 
Reply
#5

It all depends on how you are sending the json encoded data to "server b". I'd just use a CURL request and use the POST type, or an ajax POST request. Then server b just checks for the $_POST['whatever'] in a controller, and runs json_decode() on it to get it back to a regular php array.
Reply
#6

thanks for responses, creanium and cronix that sounds like best plan.

i am also trying to understand HTTP headers, body etc in terms of APIs. I googled and found this reference on stackoverflow  -- i think this is what i was imagining would happen

To access the entity body of a POST or PUT request (or any other HTTP method):

$entityBody = file_get_contents('php://input');
Also, the STDIN constant is an already-open stream to php://input, so you can alternatively do:

$entityBody = stream_get_contents(STDIN);

http://stackoverflow.com/questions/89458...ost-in-php


so i'm trying to understand the difference  -- with sending the JSON in a specific named post field - then server B is just looking for the same post field.

VERSUS the above -- is grabbing everything from the HTTP body?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB