我试图在支付接口网站上接收JSON POST,但我无法解码。

打印时:

echo $_POST;

我得到:

Array

当我试着这样做时,我什么也得不到:

if ( $_POST ) {
    foreach ( $_POST as $key => $value ) {
        echo "llave: ".$key."- Valor:".$value."<br />";
    }
}

当我试着这样做时,我什么也得不到:

$string = $_POST['operation'];
$var = json_decode($string);
echo $var;

当我尝试这个时,我得到NULL:

$data = json_decode( file_get_contents('php://input') );
var_dump( $data->operation );

当我这样做的时候:

$data = json_decode(file_get_contents('php://input'), true);
var_dump($data);

我得到:

NULL

JSON格式为(根据支付网站文档):

{
   "operacion": {
       "tok": "[generated token]",
       "shop_id": "12313",
       "respuesta": "S",
       "respuesta_details": "respuesta S",
       "extended_respuesta_description": "respuesta extendida",
       "moneda": "PYG",
       "monto": "10100.00",
       "authorization_number": "123456",
       "ticket_number": "123456789123456",
       "response_code": "00",
       "response_description": "Transacción aprobada.",
       "security_information": {
           "customer_ip": "123.123.123.123",
           "card_source": "I",
           "card_country": "Croacia",
           "version": "0.3",
           "risk_index": "0"
       }
    }
}

支付网站日志显示一切正常。有什么问题吗?


当前回答

$data = file_get_contents('php://input');
echo $data;

这对我很管用。

其他回答

Try;

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data["operacion"];

从您的json和代码中,看起来您已经正确地拼写了operation这个单词,但它不是在json中。

EDIT

也许还值得尝试从php://input中回显json字符串。

echo file_get_contents('php://input');
$data = file_get_contents('php://input');
echo $data;

这对我很管用。

由于php魔术引号,解码可能会失败(并返回null)。

如果magic quotes打开,则从_POST/_REQUEST/etc读取任何内容。将有特殊字符,如“\”,也是JSON转义的一部分。尝试json_decode(此转义字符串将失败。这是一个已弃用的功能,但仍在一些主机上启用。

检查魔术引号是否打开,如果打开就删除它们:

function strip_magic_slashes($str) {
    return get_magic_quotes_gpc() ? stripslashes($str) : $str;
}

$operation = json_decode(strip_magic_slashes($_POST['operation']));

你可以使用波纹像.. 像下面这样发布JSON

从php项目中获取数据用户咆哮喜欢

// takes raw data from the request 
$json = file_get_contents('php://input');
// Converts it into a PHP object 
$data = json_decode($json, true);

 echo $data['requestCode'];
 echo $data['mobileNo'];
 echo $data['password'];

如果以上所有答案仍然导致POST输入为NULL,请注意在本地主机设置中使用POST/JSON,这可能是因为您没有使用SSL。(前提是你使用tcp/tls而不是udp/quic)

//input在非https上将为空,如果你在流中有重定向,尝试在本地配置https作为标准实践,以避免安全/xss等各种问题