使用PHP接收JSON POST

Receive JSON POST with PHP

我正在尝试在付款界面网站上收到JSON POST,但无法对其进行解码。

当我打印时:

1
echo $_POST;

我得到:

我尝试这样做时一无所获:

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

我尝试这样做时一无所获:

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

我尝试这样做时得到NULL:

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

当我做:

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

我得到:

1
NULL

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "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"
       }
    }
}

付款站点日志显示一切正常。 有什么问题?

从json和代码中看,您似乎已经正确地拼写了单词operation,但是它不在json中。

编辑

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

1
echo file_get_contents('php://input');

  • 嗨,两种情况下我都看不到
  • 就其价值而言,操作是西班牙语中的拼写(给予或带有重音符号)。
  • 他的意思是,他在两个地方都没有正确拼写,无论是在两个地方的操作还是操作。
  • 在PHP 5.6之前,获取php:// input的内容只能执行一次。您的代码或框架可以在以前的某个位置打开它吗?
  • operacion是正确的,operation不是。


例如,如果您已经设置了$ _POST ['eg']之类的参数,而又不想更改它,只需执行以下操作:

1
$_POST = json_decode(file_get_contents('php://input'), true);

这将节省您将所有$ _POST更改为其他内容的麻烦,并且如果您希望删除此行,则可以使您仍然发出常规的帖子请求。


使用$HTTP_RAW_POST_DATA代替$_POST

它将为您提供POST数据。

稍后您将可以使用json_decode()对其进行解码。


值得指出的是,如果您使用json_decode(file_get_contents("php://input"))(如其他人所提到的那样),那么如果字符串不是有效的JSON,这将失败。

可以通过首先检查JSON是否有效来解决。即

1
2
3
4
5
6
7
8
9
function isValidJSON($str) {
   json_decode($str);
   return json_last_error() == JSON_ERROR_NONE;
}

$json_params = file_get_contents("php://input");

if (strlen($json_params) > 0 && isValidJSON($json_params))
  $decoded_params = json_decode($json_params);

编辑:请注意,删除上面的strlen($json_params)可能会导致细微的错误,因为当传递null或空白字符串时,json_last_error()不会更改,如下所示:
http://ideone.com/va3u8U


阅读文档:

In general, php://input should be used instead of $HTTP_RAW_POST_DATA.

如在php手册中


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

这对我有用。


我想发布一个答案,该答案也使用curl来获取内容,并使用mpdf将结果保存为pdf,这样您就可以得到典型用例的所有步骤。这只是原始代码(以便适应您的需求),但是它可以工作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// import mpdf somewhere
require_once dirname(__FILE__) . '/mpdf/vendor/autoload.php';

// get mpdf instance
$mpdf = new \Mpdf\Mpdf();

// src php file
$mysrcfile = 'http://www.somesite.com/somedir/mysrcfile.php';
// where we want to save the pdf
$mydestination = 'http://www.somesite.com/somedir/mypdffile.pdf';

// encode $_POST data to json
$json = json_encode($_POST);

// init curl > pass the url of the php file we want to pass
// data to and then print out to pdf
$ch = curl_init($mysrcfile);

// tell not to echo the results
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1 );

// set the proper headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($json) ]);

// pass the json data to $mysrcfile
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

// exec curl and save results
$html = curl_exec($ch);

curl_close($ch);

// parse html and then save to a pdf file
$mpdf->WriteHTML($html);
$this->mpdf->Output($mydestination, \Mpdf\Output\Destination::FILE);

在$ mysrcfile中,我将像这样读取json数据(如先前的回答所述):

1
2
$data = json_decode(file_get_contents('php://input'));
// (then process it and build the page source)