1

I have a simple xmlhttprequest with AJAX and want to rebuild this with PHP.

var data = {};
var payload = {
    "flags" : true,
    "codes" : true,
    "units" : true
};
data.payload = JSON.stringify(payload);
$.ajax({
    type : 'POST',
    url : 'http://httpbin.org/post',
    data : data,
    success : function(response) {
    var arr = JSON.stringify(response);
    document.getElementById('placeholder').innerHTML = arr;
    }
});

This works fine!

Now my Version with PHP:

$data = array(
    'payload' => array(
        'flags' => true,
        'codes' => true,
        'units' => true
    )
);

$options = array(
    "http" => array(
        "method" => "POST",
        "header" => "Content-Type: application/json\r\n",
        "content" => json_encode($data)
    )
);

$url = "http://httpbin.org/post";

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
var_dump($response);

When I compare the results there is a difference in the structure. Ajax looks like this:

{"form":{"payload":"{\"flags\":\"true\",

and PHP looks like this:

{"form": {},..."data": "{\"payload\":{\"flags\":\"true\",

Why is in PHP the "form" empty? I've tried it an extra Array "form", but when I look at the result there is a second "form" in the string.

2
  • Why you JSON.stringify the 'data'? $.ajax accept perfect an object directly. Commented Feb 19, 2014 at 13:49
  • I just test my request with "httpbin.org/post". But the real API I use needs a JSON string. That's the reason for the JSON.stringify. ;-) Commented Feb 19, 2014 at 13:56

2 Answers 2

1

You use improper way of building POST data.

Try this:

$payload = array(
    'flags' => true,
    'codes' => true,
    'units' => true
);

$data = array(
    'payload' => json_encode($payload)
);

$options = array(
    "http" => array(
        "method" => "POST",
        "header" => "Content-Type: application/x-www-form-urlencoded\r\n",
        "content" => http_build_query($data)
    )
);

Explanation

In your JS code POST data is an object containing one key-value pair {"payload": json} where json is json-encoded string representing payload object. This POST data object finally results (in $.ajax implementation) in a url-encoded string which look like this: payload=%7B%22flags%22%3Atrue%2C%22codes%22%3Atrue%2C%22units%22%3Atrue%7D.

Described process is exactly reproduced by my php code.

Sign up to request clarification or add additional context in comments.

2 Comments

THAT'S IT! You are a genious!!! I try to solve this since 7 hours and never came so far. But why looks the result so different? The "payload" now looks totally different in PHP as in AJAX. But it works.
@Tipo Actually POST data in your JS code result to the same look (string) as in my PHP code.
0

Have you tried to trigger json_last_error() right after encoding your array ?

$options = array(
    "http" => array(
        "method" => "POST",
        "header" => "Content-Type: application/json\r\n",
        "content" => json_encode($data)
    )
);

$error = json_last_error();
echo $error;

In case of a silent json error, maybe it could give you a clue.

Let us know :)

1 Comment

echo "Error: ".json_last_error(); // Error: 0 Seems to be ok. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.