質問するログイン新規登録

回答編集履歴

2

コードを追加

2017/07/05 05:48

投稿

shi_ue
shi_ue

スコア4437

answer CHANGED
@@ -5,4 +5,42 @@
5
5
  こちらから送信するヘッダーは、オプションに入っている、`header`なわけで、それを信用できないのだとすると、自分でサーバーを立ててリクエストを受け取ってみるのが楽でよさそうですね。
6
6
 
7
7
  簡単なのは、ビルトインウェブサーバーでリクエストヘッダを列記するプログラムを動かし、
8
- それに向かって、`file_get_contents`してみることですかね。
8
+ それに向かって、`file_get_contents`してみることですかね。
9
+
10
+ 追記
11
+ ---
12
+ ```php
13
+ <?php
14
+ // test_a.php
15
+ header("Content-type: text/plain; charset=UTF-8");
16
+ print_r(getallheaders());
17
+ ```
18
+ 上記を`php -S 127.0.0.1:8080 -t ./`で動かし
19
+ ```php
20
+ <?php
21
+ // index.php
22
+ $json = ["test"=>"this is test.","result"=>true];
23
+ $json = json_encode($json);
24
+
25
+ $context = stream_context_create(array('http' => array(
26
+ 'method' => "POST",
27
+ 'header' => "Content-Type: application/json; charset=utf-8 \r\n",
28
+ 'content' => $json
29
+ )));
30
+ header("Content-type: text/plain; charset=UTF-8");
31
+ echo file_get_contents("http://127.0.0.1:8080/test_a.php", false, $context);
32
+ ```
33
+ 上記を`php -S 127.0.0.1:8081 -t ./`で動かします。
34
+ それで、ブラウザから`http://127.0.0.1:8081/`にアクセスすると、送られたヘッダーが見られます。
35
+
36
+ ちなみに上記の結果は、
37
+ ```
38
+ Array
39
+ (
40
+ [Host] => 127.0.0.1:8080
41
+ [Connection] => close
42
+ [Content-Length] => 38
43
+ [Content-Type] => application/json; charset=utf-8
44
+ )
45
+ ```
46
+ となりました。

1

編集

2017/07/05 05:48

投稿

shi_ue
shi_ue

スコア4437

answer CHANGED
@@ -1,2 +1,8 @@
1
1
  [http://php.net/manual/ja/function.stream-context-create.php#74795](http://php.net/manual/ja/function.stream-context-create.php#74795)によると、
2
- `stream_context_create`のオプションは`https`ではなく、`http`みたいですね。
2
+ `stream_context_create`のオプションは`https`ではなく、`http`みたいですね。
3
+
4
+ なんかトンチンカンな回答になっちゃいました。
5
+ こちらから送信するヘッダーは、オプションに入っている、`header`なわけで、それを信用できないのだとすると、自分でサーバーを立ててリクエストを受け取ってみるのが楽でよさそうですね。
6
+
7
+ 簡単なのは、ビルトインウェブサーバーでリクエストヘッダを列記するプログラムを動かし、
8
+ それに向かって、`file_get_contents`してみることですかね。