PHP接收Json

I'm using knockout and I'm trying to send information to PHP, using firebug to check Network -> Headers I have this:

Request URL:http://localhost/loyalty/welcome/json/
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:85
Content-Type:application/json
Host:localhost
Origin:http://localhost
Referer:http://localhost/loyalty/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11
X-Requested-With:XMLHttpRequest
Request Payload
{"friends":[{"name":"name","isOnTwitter":false},{"name":"name","isOnTwitter":false}]}
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Wed, 15 Feb 2012 11:01:23 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By:PHP/5.3.8

The generated JSON is: {"friends":[{"name":"name","isOnTwitter":false},{"name":"name","isOnTwitter":false}]} and I have no idea how to get those values.

Here is the Ajax call:

save: function() {
        $.ajax({
            url:"http://localhost/loyalty/welcome/json/",
            type: "post",
            data: ko.toJSON(this),
            contentType: "application/json",
            success: function (result) {
                alert(result);
            }
        });

On my CodeIgniter method I have tried to receive it with $this->input->post('friends') and whatever else I could think of and no results.

I changed the Javascript to this:

$.ajax({
    url:"http://localhost/loyalty/welcome/json/",
    type: "post",
    data: {payload:ko.toJSON(this)},
    success: function (result) {
            t.value = result;
    }
});

Then, in PHP, you can access the JSON via:

<?php json_decode($_POST["payload"]); ?>

For PHP to get JSON objects you need to use json_decode().

http://php.net/manual/en/function.json-decode.php

change

data: ko.toJSON(this),

to

data: {mydata : ko.toJSON(this) },

In your file which opens up while you post on http://localhost/loyalty/welcome/json/

Read it as:

$myObj = json_decode($_POST['mydata']);

and then you can access your values as:

echo $myObj['friends'][0]['name']; or echo $myObj['friends'][0]['isOnTwitter'];

which will output name or true/false as your json code reads.

EDIT

This thread should help you -> Jquery - How to make $.post() use contentType=application/json?

As the Content-Type is "application/json" and NOT "application/x-www-form-urlencoded" the $_POST array will be empty.

So, in order to access the JSON payload in a POST request, you have to do this:

$json_data = json_decode(trim(file_get_contents('php://input')), true);    
echo($json_data['param1']);
echo($json_data['param2']);