-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.php
More file actions
45 lines (40 loc) · 1.44 KB
/
test2.php
File metadata and controls
45 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>APIテスト</title>
</head>
<body>
<h1>APIテスト</h1>
<?php
$apiResponse = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$userInput = $_POST['userInput'] ?? '';
$apiResponse = sendToAPI($userInput);
}
function sendToAPI($userInputValue) {
$apiEndpoint = 'http://localhost:8000/generate/'; // あなたのAPIエンドポイントURL
$options = [
'http' => [
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode(['text' => $userInputValue]),
]
];
$context = stream_context_create($options);
$result = file_get_contents($apiEndpoint, false, $context);
if ($result === FALSE) {
return 'エラー: ネットワークレスポンスが正常ではありません。';
} else {
$data = json_decode($result, true);
return '結果: ' . $data['result'];
}
}
?>
<form method="post">
<input type="text" name="userInput" placeholder="テキストを入力" value="<?php echo htmlspecialchars($userInput ?? '', ENT_QUOTES, 'UTF-8'); ?>">
<button type="submit">送信</button>
</form>
<div id="apiResponse"><?php echo htmlspecialchars($apiResponse, ENT_QUOTES, 'UTF-8'); ?></div>
</body>
</html>