0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

postでjsonを送って、受け取るfilter_inputやparseを使って

Last updated at Posted at 2021-03-08

phpで情報を取得して、postで送信

データを受け取って、処理に移るまで

urlに情報を送りたい(postでjsonを)

htmlのinputから情報が送られてくるのを、
$_POSTで取得できるのだが、スーパーグローバル変数はセキュリティ上よくないのでfilter_inputで取得する

複数の情報を送りたい時には、array(配列)で送信する。
$data = array('login_id' =>$login_id);

optionの中に、methodやheaderの情報を入れ込む。
contentの中は、http_build_query($data)

http_build_queryはエンコード(後に元の信号またはデータに戻せるような変換を加えることである)してくれるもので、dataを送信できるようにしている。

Content-type: application/x-www-form-urlencodedを指定しなくても動く。
使わない時には、multipart/form-data がデフォルトで通信してしまうらしい。
https://adan.jp.net/blog/programing/1792#Content-type%E3%81%AE%E3%83%88%E3%83%A9%E3%83%96%E3%83%AB%E5%9B%9E%E9%81%BFhttp_build_query%E3%81%AE%E9%96%A2%E4%BF%82%E6%80%A7

※ただしこの際、自分の環境では、Content-typeを指定しないで送信しようとすると、以下のエラーが出ました。

Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded

file_get_contentsを使用する際にはheader情報を付け加えないと、いけないらしく
headerをきちんとx-www-form-urlencodedにしてやると動きました。
https://qiita.com/suisuina/items/9b7d44118266f97a577d#comments

file_gets_contentの引数にstream_context_creare()$dataを渡してやる。
後に、file_get_contents()を使って、ファイルの内容を全て文字列に読み込む。失敗時はFalseが返される。
https://www.php.net/manual/ja/function.file-get-contents.php

ストリームコンテキストって何?

ストリームとは、なんでもファイルのように扱えることができるオブジェクトの塊。
データをやりとりするための基盤となる仕組みをストリームと呼ぶとかなんとか。 という自分の認識です。

コンテキスト は、ストリームの挙動を変えたり、 拡張したりすることのできる パラメータ と ラッパ固有の オプション の集合です。

ストリームコンテキストとは、どのようにストリームを扱うのかなどを明記するもの。
ストリームの挙動を変えたり、拡張したりすることのできるオプションの総体である。
つまり、ここではstream_context_create($options)においてoptionを渡しているので
どのようなオプションをつけてデータを送信するのかを決めている(header情報やmethodなど)
また、optionは連想配列でなければならないらしい。。

https://qiita.com/desho/items/14361eb8594faf661210
https://www.weblio.jp/content/%E3%82%B9%E3%83%88%E3%83%AA%E3%83%BC%E3%83%A0%E9%96%A2%E6%95%B0

以下コード

$url = 'localhost:5000/login/';
    $login_id = filter_input(INPUT_POST,'login_id');
    $data = array(
        'login_id'=>$login_id
    );
    $options = array(
        'http'=>array(
            'method'=>'POST',
            'header' =>'Content-type: application/x-www-form-urlencoded',
            'content' => http_build_query($data)
        )
    );
    $received_data = file_get_contents($url, false, stream_context_create($options));

受け取り

$input_data = file_get_contents("php://input");
    parse_str($input_data,$data_array);
    $login_id = $data_array['login_id'];

postで送信されたjsonを取得する際にはfile_get_contents("php://input")を用いる。
パースする。
パースとは、文法に従って分析する、構造解析するなどの意味。

送信されるデータは、urlクエリパラメータで送られてくるが、より使いやすくするためにparse_strを用いる。使うと、input_dataの内容を$data_arrayに配列の要素として変数に保存され扱いやすくなる。https://www.php.net/manual/ja/function.parse-str.php

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?