yii2中post如何接收JSON資料?

2021-09-25 09:48:00 字數 391 閱讀 9364

yiii2中開發的介面,在postman中使用x-www-format-unencoded的格式進行post請求測試,可以正確返回想要的結果。

但是選用content-type為text/json時,yii控制器中根本沒有獲取到資料,怎麼設定呢。

1.需要在yii2的配置檔案的components 中的request陣列裡加上以下的轉換器:

2.用php://input接收

$post = @file_get_contents('php://input'); 

//獲取請求體,@的作用為遮蔽警告,可去除。

$post = json_decode( $post, true );

//解析成陣列

如何歸檔安裝Yii2框架

2.接下來鏈結資料庫,先建立乙個資料庫blogdemo2db,開啟配置檔案common config main local.php 設定dbname blogdemo2db,username root,password root 4.美化url 暫時不做 2018.3.9新增 mac 下如何歸檔安裝...

Yii2 中cookie的用法(2)

設定cookie phpsetcookie name larry time 3600 yii2 cookies add new yii web cookie name name value larry expire time 3600 建立名為 name 的cookie,賦值為 larry 並且規定...

yii2中的多表查詢

資料庫 classe表 create table classe cid int 11 not null auto increment,cname varchar 10 default null,tid int 11 default null,primary key cid key fk id cla...