PHP API用のHeader Json 出力

PHPでAPI用にJSONを出力する

PHPでJSON用のヘッダーを出力し、json_encode() を使用して出力すると、
Javascriptや他の言語でリクエストした時に値を受け取ることができます。

サンプルプログラム


header("Content-Type: application/json; charset=utf-8");
$message = ['message'=>'ok'];
echo json_encode($message);

まとめ

ヘッダーにコンテンツタイプを設定するとPOST MAN等できれいに表示できます。

json_encodeすることで配列をそのままJSON形式に変換することができます。

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x