php 是否可以通过post发送json数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13261615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:03:47  来源:igfitidea点击:

Is it possible to send a json array via post?

phphtmljsonpost

提问by user962449

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

我正在尝试将 json 数组发送到 php post 请求。

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

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

如何将 $myarray json 数据发送到 php post 请求?

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?

我正在测试 API 并被告知它只需要 json 数据......但我似乎无法让它工作。我的猜测是我发送的 json 数据错误。有什么想法吗?

回答by Sheac

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

您遇到的问题是此字符串不是正确的 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.

这将是正确的 JSON [500,"hello world"]。JSON 对格式非常严格,要求所有字符串值都用双引号括起来,切勿用单引号括起来。

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

避免问题的正确做法是使用 php 函数json_encode()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,

在 post.php 中你会像这样阅读它,

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

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

truejson_decode()告诉你想把它当作一个关联数组,而不是一个PHP对象,这是它的默认行为的功能。

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

print_r()函数将输出转换后的 JSON 数组的 php 结构:

Array(
    [0] => 500
    [1] => hello world
) 

回答by Yuri Teixeira

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

API 是第 3 方提供的还是由您制作的?

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

如果它是您的,那么在您的 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.

如果是第 3 方,他们可能希望您在帖子正文中发送 json。要做到这一点,请关注这篇文章:如何使用 curl 将 JSON 发布到 PHP