回答編集履歴

2

コードを追加

2017/07/05 05:48

投稿

shi_ue
shi_ue

スコア4437

test CHANGED
@@ -13,3 +13,79 @@
13
13
  簡単なのは、ビルトインウェブサーバーでリクエストヘッダを列記するプログラムを動かし、
14
14
 
15
15
  それに向かって、`file_get_contents`してみることですかね。
16
+
17
+
18
+
19
+ 追記
20
+
21
+ ---
22
+
23
+ ```php
24
+
25
+ <?php
26
+
27
+ // test_a.php
28
+
29
+ header("Content-type: text/plain; charset=UTF-8");
30
+
31
+ print_r(getallheaders());
32
+
33
+ ```
34
+
35
+ 上記を`php -S 127.0.0.1:8080 -t ./`で動かし
36
+
37
+ ```php
38
+
39
+ <?php
40
+
41
+ // index.php
42
+
43
+ $json = ["test"=>"this is test.","result"=>true];
44
+
45
+ $json = json_encode($json);
46
+
47
+
48
+
49
+ $context = stream_context_create(array('http' => array(
50
+
51
+ 'method' => "POST",
52
+
53
+ 'header' => "Content-Type: application/json; charset=utf-8 \r\n",
54
+
55
+ 'content' => $json
56
+
57
+ )));
58
+
59
+ header("Content-type: text/plain; charset=UTF-8");
60
+
61
+ echo file_get_contents("http://127.0.0.1:8080/test_a.php", false, $context);
62
+
63
+ ```
64
+
65
+ 上記を`php -S 127.0.0.1:8081 -t ./`で動かします。
66
+
67
+ それで、ブラウザから`http://127.0.0.1:8081/`にアクセスすると、送られたヘッダーが見られます。
68
+
69
+
70
+
71
+ ちなみに上記の結果は、
72
+
73
+ ```
74
+
75
+ Array
76
+
77
+ (
78
+
79
+ [Host] => 127.0.0.1:8080
80
+
81
+ [Connection] => close
82
+
83
+ [Content-Length] => 38
84
+
85
+ [Content-Type] => application/json; charset=utf-8
86
+
87
+ )
88
+
89
+ ```
90
+
91
+ となりました。

1

編集

2017/07/05 05:48

投稿

shi_ue
shi_ue

スコア4437

test CHANGED
@@ -1,3 +1,15 @@
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
2
 
3
3
  `stream_context_create`のオプションは`https`ではなく、`http`みたいですね。
4
+
5
+
6
+
7
+ なんかトンチンカンな回答になっちゃいました。
8
+
9
+ こちらから送信するヘッダーは、オプションに入っている、`header`なわけで、それを信用できないのだとすると、自分でサーバーを立ててリクエストを受け取ってみるのが楽でよさそうですね。
10
+
11
+
12
+
13
+ 簡単なのは、ビルトインウェブサーバーでリクエストヘッダを列記するプログラムを動かし、
14
+
15
+ それに向かって、`file_get_contents`してみることですかね。