[英]Receive JSON POST with PHP
我正在嘗試在支付接口網站上接收 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 = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data["operacion"];
從您的 json 和您的代碼來看,您似乎已經正確拼寫了operation一詞,但它不在 json 中。
編輯
也許還值得嘗試從 php://input 回顯 json 字符串。
echo file_get_contents('php://input');
如果您已經設置了諸如 $_POST['eg'] 之類的參數,並且您不想更改它,只需這樣做:
$_POST = json_decode(file_get_contents('php://input'), true);
這將為您省去將所有 $_POST 更改為其他內容的麻煩,並且如果您希望刪除此行,您仍然可以發出正常的 post 請求。
值得指出的是,如果您使用json_decode(file_get_contents("php://input"))
(正如其他人所提到的),如果字符串不是有效的 JSON,這將失敗。
這可以通過首先檢查 JSON 是否有效來簡單地解決。 IE
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)
可能會導致細微的錯誤,因為json_last_error()
在傳遞null
或空白字符串時不會改變,如下所示:http: //ideone.com/va3u8U
使用$HTTP_RAW_POST_DATA
而不是$_POST
。
它會按原樣為您提供 POST 數據。
稍后您將能夠使用json_decode()
對其進行解碼。
$data = file_get_contents('php://input');
echo $data;
這對我有用。
很晚了。
看來,(OP)已經嘗試了所有給他的答案。
不過,如果您(OP)沒有收到傳遞給“.PHP”文件的內容,則錯誤可能是 URL 不正確。
檢查您是否調用了正確的“.PHP”文件。
(網址中的拼寫錯誤或大寫字母)
最重要的是
檢查您的 URL 在“http”之后是否有“s”(安全)。
例子:
"http://yourdomain.com/read_result.php"
應該
"https://yourdomain.com/read_result.php"
或任何一種方式。
添加或刪除“s”以匹配您的 URL。
如果以上所有答案仍然導致您為 POST 輸入 NULL,請注意 localhost 設置中的 POST/JSON,這可能是因為您沒有使用 SSL。 (前提是您是帶有 tcp/tls 而不是 udp/quic 的 HTTP)
PHP://input 將在非 https 上為空,如果您在流程中有重定向,請嘗試在本地配置 https 作為標准做法以避免各種安全/xss 等問題
由於 php 魔術引號,解碼可能會失敗(並返回 null)。
如果魔術引號打開了從 _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']));
當我嘗試在 php 中檢索發布的數據時,我得到了“null”
{
"product_id": "48",
"customer_id": "2",
"location": "shelf", // shelf, store <-- comments create php problems
"damage_types":{"Pests":1, "Poke":0, "Tear":0}
// "picture":"jhgkuignk" <-- comments create php problems
}
避免注釋 json 代碼,即使它沒有顯示錯誤
我想發布一個答案,該答案也使用 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.