发回json数据

时间:2014-08-31 09:41:13

标签: php arrays json post

我正在尝试将一些json数据发送回服务器但到目前为止没有任何工作D:

我要做的第一件事是通过执行以下操作来获取数据:

$oldData = file_get_contents('dataset/dataset.json');
$oldData = json_decode($oldData, true);
$oldBlock = $oldData['data'][0]['blocks'];

现在我更改了一些值并希望将更改后的内容发回,但我不知道该怎么做。我所知道的是我必须再次对数组进行编码。

谢谢

1 个答案:

答案 0 :(得分:0)

你可以在php中使用cURL:

$url = ""; // whatever url
$headers = array(
    'Content-Type: application/json'
);

$newBlock = json_encode(...); // your new json to send

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($newBlock));

$result = curl_exec($ch);
curl_close($ch);

这应该可以解决问题。