GET 請求作業正常,但如果我嘗試 POST 發送... nginx vhost conf
location /api/b1/ {
auth_request /_validate_apikey;
try_files $uri $uri/ /api.php?$args;
rewrite ^/api/b1/([^/] )/([^/] )/?$ /api.php?data1=$1&data2=$2? last;
}
nginx日志
185.111.90.223 - - [28/Oct/2022:13:16:59 0200] "GET /api/b1/1951/ugyfel_modul HTTP/1.1" 200 16039 "-" "-"
185.111.90.223 - - [28/Oct/2022:13:16:59 0200] "POST /api/b1/1951/adatkuldes HTTP/1.1" 200 19 "-" "-"
請求顯然沒問題,沒有令牌錯誤,但是 api.php 你沒有得到 POST 陣列。
php curl 呼叫
$array=json_encode($array,JSON_FORCE_OBJECT);
$ch = curl_init($server.'/'.$data1.'/'.$data2);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($array),
'X-API-KEY: '.$token)
);
$result = curl_exec($ch); curl_close($ch);
$api = json_decode($result, true);
遠程代碼:
if(!empty($_POST)) {
$json = array("ok"=>1);
}
else {
$json = array("ok"=>0);
}
答案是:ok=>0
未收到 POST 資料的原因可能是什么?我沒有注意到什么?我的意思是 nginx 配置錯誤....
uj5u.com熱心網友回復:
僅當您使用 application/x-www-form-urlencoded 或 multipart/form-data 作為 HTTP Content-Type 發送資料時才會填充 POST 變數(查看手冊https://www.php.net/manual/en/ reserved.variables.post.php)。嘗試:
$data = json_decode(file_get_contents('php://input'), true);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/522613.html
標籤:phpapinginx
