我试图在支付接口网站上接收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"
}
}
}
支付网站日志显示一切正常。有什么问题吗?
我想发布一个答案,它也使用curl来获取内容,并使用mpdf将结果保存为pdf,这样您就可以获得典型用例的所有步骤。它只是原始代码(因此可以根据您的需要进行调整),但它可以工作。
// 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数据(如前所述):
$data = json_decode(file_get_contents('php://input'));
// (then process it and build the page source)
我想发布一个答案,它也使用curl来获取内容,并使用mpdf将结果保存为pdf,这样您就可以获得典型用例的所有步骤。它只是原始代码(因此可以根据您的需要进行调整),但它可以工作。
// 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数据(如前所述):
$data = json_decode(file_get_contents('php://input'));
// (then process it and build the page source)