2

Im trying to send a json array to a php post request.

$myarray[] = 500;
$myarray[] = "hello world";

how can I send $myarray json data to a php post request?

Here's what I tried:

<form name="input" action="post.php">
<input type="hidden" name="json" value="[500,'hello world']" />
<input type="submit" value="Submit">
</form>

Im testing the API and was told it only takes json data...but I can't seem to get it to work. My guess is Im sending the json data wrong. Any thoughts?

user962449
  • 3,743
  • 9
  • 38
  • 53
  • 3
    JSON objects are text. Yes, you can send text via `post`. –  Nov 07 '12 at 00:36
  • 2
    http://php.net/manual/en/function.json-decode.php – sachleen Nov 07 '12 at 00:36
  • 1
    @JackManey - [Well, actually,](http://tirania.org/blog/archive/2011/Feb-17.html) there's no such a thing as a JSON object. All there is [is a JSON string](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Joseph Silber Nov 07 '12 at 00:42
  • How would I modify my code to send a JSON string? – user962449 Nov 07 '12 at 00:47
  • @user962449 just set the value of an `input` to the json encoded string and use `json_decode` in PHP to decode it to an object. – sachleen Nov 07 '12 at 00:49
  • That's what I did....but it's not working. The problem is not on the receiving end, but on the sending one. I think Im sending the data formatted wrong, but I used json_encode to encode the array and set it as the value for the json input, as noted above. – user962449 Nov 07 '12 at 00:51
  • 1
    If the API expects a JSON-encoded request, none of this is going to work. You're POSTing a form (content type `application/x-www-form-urlencoded`) where one of the fields is JSON-encoded, but that's probably not what the API wants. – cmbuckley Nov 07 '12 at 01:18

2 Answers2

3

The problem your having is this string is NOT Proper JSON: [500,'hello world']

this would be proper JSON [500,"hello world"]. JSON is very strict on formatting and requires that all string values be wrapped in double quotes and NEVER single quotes.

the proper thing to do to avoid problems, would be to use the php functions json_encode() and json_decode()

for example,

<?php
    $myarray[] = 500;
    $myarray[] = "hello world";
    $myjson = json_encode($myarray);
?>
<form name="input" action="post.php">
    <input type="hidden" name="json" value="<?php echo $myjson ?>" />
    <input type="submit" value="Submit">
</form>

and in the post.php you would read it like so,

<?php
    $posted_data = array();
    if (!empty($_POST['json'])) {
        $posted_data = json_decode($_POST['json'], true);
    }
    print_r($posted_data);
?>

the true flag in json_decode() tells the function you want it as an associative array and not a PHP object, which is its default behavior.

the print_r() function will output the php structure of the converted JSON array:

Array(
    [0] => 500
    [1] => hello world
) 
Sheac
  • 438
  • 2
  • 6
  • 3
    Good catch. However you also need to use single quotes then for the `value='..'` attribute, or better yet apply `htmlspecialchars()`, so the JSONs double quotes don't break the HTML syntax. – mario Nov 07 '12 at 01:18
  • oops, yeah was going a bit fast, good catch yourself. forgot about the quotes on the input tag lol. If you wanted to avoid the special characters and avoid the single quotes on the input, you could just `base64_encode` the json, then on the other end base64_decode it to avoid any complications. similar to how facebook does their posted json signed request on load of a facebook application. – Sheac Nov 07 '12 at 01:29
1

Is the API a 3rd party on or made by you?

If it is yours, consume the data sent by your form should be as simple as this on your API:

<?php
    $data = json_decode($_REQUEST['json']);

    echo $data[0]; // Should output 500
    echo $data[1]; // Should output hello world

If it is a 3rd party, probably they expect you to send the json in post body. To accomplish that, follow this post: How to post JSON to PHP with curl.

Community
  • 1
  • 1