Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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回调获取POST数据_Php_Json - Fatal编程技术网

从php中的json回调获取POST数据

从php中的json回调获取POST数据,php,json,Php,Json,我使用的API使用json POST请求向我设置的页面发送回调 回调的示例: { "order": { "id": "5RTQNACF", "created_at": "2012-12-09T21:23:41-08:00", "status": "completed", "total_btc": { "cents": 100000000, "currency_iso": "BTC" }, "custom": "order1234", } 我需要知道如何获取这些参数并将它们设置为php

我使用的API使用json POST请求向我设置的页面发送回调

回调的示例:

{
"order": {
"id": "5RTQNACF",
"created_at": "2012-12-09T21:23:41-08:00",
"status": "completed",
"total_btc": {
  "cents": 100000000,
  "currency_iso": "BTC"
},

"custom": "order1234",
}
我需要知道如何获取这些参数并将它们设置为php变量

用于获取此字符串并获取数组:

$array = json_decode($jsonString, true);
如果您确实希望将这些作为单个php变量,请使用:

例如:

请记住,
extract
可能会覆盖其他局部变量,这通常不是好的做法。我建议您访问
$array
中所需的数据

更新:听起来您首先很难访问发布JSON字符串。有两种可能性:

  • 如果数据是作为普通的POST键/值对发送的,您可以在页面上执行
    print\r($\u POST)
    ,您应该可以看到json字符串的位置

  • 它可能是作为原始POST数据而不是键/值对发送的。在这种情况下,您需要查看
    file\u get\u内容(“php://input");。试着重复一下。如果找到JSON字符串,那么只需设置
    $jsonString=file\u get\u contents(“php://input");
    并继续执行下一步的
    json\u解码


  • 它们将出现在
    $\u POST
    不鼓励提取,因为它可能会覆盖局部变量
    json\u解码
    并以数组/嵌套数组的形式访问结果元素。@aularon完全同意。这就是为什么我说“如果你真的想……”最好访问数组,但我想回答他的问题。json字符串就贴在我的回拨页面上。所以我无法设置$jsonString。我可以用解析表单数据的方法检索参数并设置变量吗?例如,$order=$_POST[“order”];还是我在这里偏离了正轨?你需要对字符串进行解码。如何访问要使用的字符串<代码>$\u POST['something']
    ?那怎么办?我的困惑在于“$array=json_decode($jsonString,true);”,而“$array”是json代码本身。如果将json发布到页面,如何设置“$array”变量?
    extract($array); // now you have a local $order variable
    echo $order['custom']; // etc...
    
    extract($order); // now you have local variables $id, $status, etc
    echo $id;