質問編集履歴

2

コードの変更

2023/01/28 14:11

投稿

submaru
submaru

スコア18

test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,7 @@
8
8
  絶対URLだとうまくいくのですが,相対URLで実現したく,またこのような状態になっている理由を知りたい.
9
9
 
10
10
  ```send.php
11
+ <?php
11
12
  $body = array("hoge" => "hoge", "hoge2" => "hoge2");
12
13
  $body = json_encode($body);
13
14
  $options = array(
@@ -21,6 +22,7 @@
21
22
  ```
22
23
 
23
24
  ```receive.php
25
+ <?php
24
26
  $dsn = "mysql:host=localhost; dbname=○○";
25
27
  $username = "○○";
26
28
  $password = "○○";

1

コードの追加

2023/01/28 14:10

投稿

submaru
submaru

スコア18

test CHANGED
File without changes
test CHANGED
@@ -7,6 +7,33 @@
7
7
  MySQLのデータベースへINSERTを行いたいので,「->」を利用できるようにしたい.
8
8
  絶対URLだとうまくいくのですが,相対URLで実現したく,またこのような状態になっている理由を知りたい.
9
9
 
10
+ ```send.php
11
+ $body = array("hoge" => "hoge", "hoge2" => "hoge2");
12
+ $body = json_encode($body);
13
+ $options = array(
14
+ "http" => array(
15
+ "method" => "POST",
16
+ "header" => "Content-Type: application/json; charset=UTF-8",
17
+ "content" => $body
18
+ )
19
+ );
20
+ echo file_get_contents("receive.php", false, stream_context_create($options));
21
+ ```
22
+
23
+ ```receive.php
24
+ $dsn = "mysql:host=localhost; dbname=○○";
25
+ $username = "○○";
26
+ $password = "○○";
27
+
28
+ $json = file_get_contents("php://input");
29
+ $data = json_decode($json, true);
30
+
31
+ $pdo = new PDO($dsn, $username, $password);
32
+ $sth = $pdo->prepare("INSERT INTO test (test) VALUES (?)");
33
+ $sth->execute(array($data));
34
+ $pdo = null;
35
+ ```
36
+
10
37
  ### 補足情報(FW/ツールのバージョンなど)
11
38
 
12
39
  WebサーバはApacheを使用しています.