Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何以以下格式将数据发布为JSON_Php_Json_Post - Fatal编程技术网

Php 如何以以下格式将数据发布为JSON

Php 如何以以下格式将数据发布为JSON,php,json,post,Php,Json,Post,我需要发送以下格式的POST数据 { "buys": [ { "productId": "1ae9ac1a37934fde92d7545cd6c93c13", "amount": 200.00 } ] } 目前,表单提交到的PHP脚本是 <?php $productId = $_POST['productId']; $amount = $_POST['amount']; $type = $_POST['name']; //API Url

我需要发送以下格式的POST数据

{
  "buys": [
    {
      "productId": "1ae9ac1a37934fde92d7545cd6c93c13",
      "amount": 200.00
    }
  ]
}
目前,表单提交到的PHP脚本是

<?php
$productId = $_POST['productId'];
$amount = $_POST['amount'];
$type = $_POST['name'];

//API Url
$url = 'https://********.com/post_test.php';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
    'type' => $type,
    'productId' => $productId,
    'amount' => $amount
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

//Execute the request
$result = curl_exec($ch);

?>
我真的不知道下一步该做什么,也不知道用谷歌搜索什么来找到教程

更多细节,我有一个表单提交给上面的PHP脚本。变量$name是请求格式中的“buys”,其他变量是不言自明的


我确信有一种方法我需要使用我的变量格式化JSON,但我不知道从哪里开始。

您的JSON数据应该是这样的

$jsonData=array(
$type=>array(
“productId”=>$productId,
“金额”=>美元金额
)
);
输出:

{“忙”:{“产品ID”:“1AE9AC1A379334FDE92D7545CD6C93C13”,“金额”:“200.00”}


您的json数据应该如下所示

$jsonData=array(
$type=>array(
“productId”=>$productId,
“金额”=>美元金额
)
);
输出:

{“忙”:{“产品ID”:“1AE9AC1A379334FDE92D7545CD6C93C13”,“金额”:“200.00”}


因此,您希望成为一个数组:

//The JSON data.
$jsonData = array(
    $type =>
    array(
        'productId' => $productId,
        'amount' => $amount
    )
);

输出将是
{“bugs”:{“productId”:“productId”,“amount”:“amount”}

因此您希望成为一个数组:

//The JSON data.
$jsonData = array(
    $type =>
    array(
        'productId' => $productId,
        'amount' => $amount
    )
);

输出将是
{“bug”:{“productId”:“productId”,“amount”:“amount”}

更改您的
$jsonData

$jsonData=array(
'type'=>$type,
“productId”=>$productId,
“金额”=>美元金额
);
详情如下:

$jsonData=array();
$jsonData[$type]=array();
$jsonData[$type][]=数组(
“productId”=>$productId,
“金额”=>美元金额
);

更改您的
$jsonData

$jsonData=array(
'type'=>$type,
“productId”=>$productId,
“金额”=>美元金额
);
详情如下:

$jsonData=array();
$jsonData[$type]=array();
$jsonData[$type][]=数组(
“productId”=>$productId,
“金额”=>美元金额
);
您可以试试这个

   $jsonData['buys'][] = array(
         'productId' => $productId,
        'amount' => $amount
    );
你可以试试这个

   $jsonData['buys'][] = array(
         'productId' => $productId,
        'amount' => $amount
    );

print_r(json_decode(您的json_字符串,true))
将显示您需要的数组
print_r(json_decode(您的_json_字符串,true))将向您显示所需的数组谢谢,现在我已经显示了这一点。每天学习谢谢你,现在我明白了。每天都在学习谢谢你,我本来是冲着我标记为正确的答案发飙的。这就是我所需要的,因为它有[]谢谢你,我最初是冲着我标记为正确的答案发火的。这是我需要的,因为它有[]