如何在HTML/PHP表单中使用PUT方法传递用户输入值?
实现PHP客户端通过PUT方法传递用户输入值
没问题,我帮你调整代码实现PUT请求传递用户输入的需求~你的现有代码是用file_get_contents做GET请求获取数据,要改成PUT请求的话,有两种简单可靠的实现方式,我都给你整理好了:
方式一:用file_get_contents实现PUT请求
这种方式和你现有代码风格更接近,通过创建请求上下文来指定PUT方法和请求体:
<!DOCTYPE html> <html> <body> <?php // 处理用户提交的输入(假设是表单提交的数据) $userInput = isset($_POST['produk_input']) ? $_POST['produk_input'] : ''; // 如果服务需要JSON格式,把输入转成JSON字符串(根据服务要求调整格式) $requestPayload = json_encode(['user_data' => $userInput]); $ip = '172.27.40.113'; // 原有的GET请求部分保留(如果需要获取数据的话) $url = 'http://'.$ip.'/service/aca/public/all-produk-by-id/2'; $string = file_get_contents($url); $json_a = json_decode($string, true); // 要发送PUT请求的目标URL $reqUrl = 'http://'.$ip.'/service/aca/public/request-produk-branch/2'; // 创建PUT请求的上下文配置 $contextOptions = [ 'http' => [ 'method' => 'PUT', // 指定请求方法为PUT 'header' => "Content-Type: application/json\r\n" . // 设置内容类型 "Content-Length: " . strlen($requestPayload) . "\r\n", // 设置内容长度 'content' => $requestPayload // 把用户输入作为请求体传递 ] ]; // 创建请求上下文 $context = stream_context_create($contextOptions); // 发送PUT请求并处理响应 if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $response = file_get_contents($reqUrl, false, $context); if ($response === false) { $error = error_get_last(); echo '<p style="color:red;">PUT请求失败:' . $error['message'] . '</p>'; } else { $responseData = json_decode($response, true); echo '<p style="color:green;">PUT请求成功!响应数据:</p>'; echo '<pre>' . print_r($responseData, true) . '</pre>'; } } catch (Exception $e) { echo '<p style="color:red;">请求异常:' . $e->getMessage() . '</p>'; } } ?> <!-- 用户输入表单 --> <form method="post"> <label>输入要传递的值:</label> <input type="text" name="produk_input" value="<?php echo htmlspecialchars($userInput); ?>"> <button type="submit">发送PUT请求</button> </form> <!-- 原有的数据展示表格 --> <table border="1"> <th width="50">ID</th> <th>产品名称</th> <!-- 根据你的$json_a结构补充表格内容 --> <?php if (!empty($json_a)): ?> <?php foreach ($json_a as $produk): ?> <tr> <td><?php echo $produk['id']; ?></td> <td><?php echo $produk['name']; ?></td> </tr> <?php endforeach; ?> <?php endif; ?> </table> </body> </html>
关键说明:
- 用
stream_context_create创建包含PUT方法的请求上下文,这是让file_get_contents支持非GET请求的核心 Content-Type和Content-Length头要根据服务端的要求设置,如果服务接受表单格式,就改成application/x-www-form-urlencoded- 把用户输入的内容作为
content参数传入,这就是PUT请求的请求体数据
方式二:用CURL实现PUT请求(更灵活)
如果你的需求更复杂(比如需要处理HTTPS、超时、认证等),用CURL会更可靠:
<!DOCTYPE html> <html> <body> <?php // 处理用户提交的输入 $userInput = isset($_POST['produk_input']) ? $_POST['produk_input'] : ''; $requestPayload = json_encode(['user_data' => $userInput]); $ip = '172.27.40.113'; // 原有的GET请求部分 $url = 'http://'.$ip.'/service/aca/public/all-produk-by-id/2'; $string = file_get_contents($url); $json_a = json_decode($string, true); $reqUrl = 'http://'.$ip.'/service/aca/public/request-produk-branch/2'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $ch = curl_init($reqUrl); // 设置PUT请求方法 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // 设置请求体 curl_setopt($ch, CURLOPT_POSTFIELDS, $requestPayload); // 让CURL返回响应内容而不是直接输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 设置请求头 curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($requestPayload) ]); // 执行请求 $response = curl_exec($ch); if (curl_errno($ch)) { echo '<p style="color:red;">PUT请求失败:' . curl_error($ch) . '</p>'; } else { $responseData = json_decode($response, true); echo '<p style="color:green;">PUT请求成功!响应数据:</p>'; echo '<pre>' . print_r($responseData, true) . '</pre>'; } // 关闭CURL资源 curl_close($ch); } ?> <!-- 用户输入表单 --> <form method="post"> <label>输入要传递的值:</label> <input type="text" name="produk_input" value="<?php echo htmlspecialchars($userInput); ?>"> <button type="submit">发送PUT请求</button> </form> <!-- 数据展示表格 --> <table border="1"> <th width="50">ID</th> <th>产品名称</th> <?php if (!empty($json_a)): ?> <?php foreach ($json_a as $produk): ?> <tr> <td><?php echo $produk['id']; ?></td> <td><?php echo $produk['name']; ?></td> </tr> <?php endforeach; ?> <?php endif; ?> </table> </body> </html>
关键说明:
- CURL支持更多高级配置,比如设置超时时间
CURLOPT_TIMEOUT、处理HTTPS的证书验证CURLOPT_SSL_VERIFYPEER等 - 如果服务需要认证,直接在请求头里添加
Authorization字段即可
你可以根据自己的实际需求选择其中一种方式,记得根据服务端要求调整请求体的格式(比如JSON、表单格式)和请求头哦~
内容的提问来源于stack exchange,提问作者Gerald Sihotang
相关产品推荐
相关产品推荐

