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 |
我尝试这样做时得到NULL:
1 2 |
当我做:
1 2 |
我得到:
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 |
- 嗨,两种情况下我都看不到
- 就其价值而言,操作是西班牙语中的拼写(给予或带有重音符号)。
- 他的意思是,他在两个地方都没有正确拼写,无论是在两个地方的操作还是操作。
- 在PHP 5.6之前,获取php:// input的内容只能执行一次。您的代码或框架可以在以前的某个位置打开它吗?
-
operacion 是正确的,operation 不是。
例如,如果您已经设置了$ _POST ['eg']之类的参数,而又不想更改它,只需执行以下操作:
1 |
这将节省您将所有$ _POST更改为其他内容的麻烦,并且如果您希望删除此行,则可以使您仍然发出常规的帖子请求。
使用
它将为您提供POST数据。
稍后您将可以使用
值得指出的是,如果您使用
可以通过首先检查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); |
编辑:请注意,删除上面的
http://ideone.com/va3u8U
阅读文档:
In general, php://input should be used instead of $HTTP_RAW_POST_DATA.
如在php手册中
1 2 |
这对我有用。
我想发布一个答案,该答案也使用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) |