PHP 抓取沒有名字的 POST 值的方法

..

現在很流行使用 JSON 的傳資料,更懶惰的是連名字都沒有指定,直接將值 json_ecode() 後,直接整個 POST 過來,但是這樣子 $POST 沒有名字就無法抓取值,要怎麼做呢?

PHP 抓取沒有名字的 POST 值的方法

想要抓取沒有名稱的 $POST,可以使用 file_get_contents('php://input') 來抓,要順便 json_decode() 的完整寫法如下:

  • $POST = jsondecode(file_get_contents('php://input'), true);

範例程式

  • vim post_raw.php
     
  • vim post_raw_send.php
     "abc", "b" => "def"]'));  function post_url($url, $postvar) {     $ch = curl_init();     curl_setopt($ch, CURLOPT_URL, $url);     curl_setopt($ch, CURLOPT_POST, 1);     curl_setopt($ch, CURLOPT_POSTFIELDS, $postvar);     $res = curl_exec($ch);     curl_close($ch);      return $res; } ?>
  • php post_raw_send.php
    • string(28) "["a" => "abc", "b" => "def"]"
想在手機閱讀更多PHP資訊?下載【香港矽谷】Android應用
分享到Facebook