我正在尝试使用stream_context_create()函数发送POST请求,但我没有得到预期的输出。
流程(代码如下):
代码:
的的index.php
<html>
<body>
<form method="POST" action="submit.php">
<input type="text" name="field" />
<button id="submit">Upload</button>
<input type="hidden" name="submit" />
</form>
<script>
document.getElementById('submit')
.addEventListener('click', function(){
document.querySelector('form').submit();
}, false);
</script>
</body>
</html>
submit.php
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST'){
echo "<input type='text' value='value = " . $_POST['field'] . "' />";
}
else {
echo "form not submitted correctly";
}
?>
upload.php的
<?php
$url = 'submit.php';
$data = array('field' => 'Check1');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$fp = fopen('submit.php', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>
加载upload.php时收到的输出
"; } else { echo "form not submitted correctly"; } ?>
/*******************************************************/
我不知道出了什么问题?提交POST请求的任何其他解决方案都已被确认。
答案 0 :(得分:0)
问题在于网址。将网址从submit.php更改为http://localhost/submit.php完成了如下所示的工作:
$fp = fopen('submit.php', 'r', false, $context);
到
$fp = fopen('http://localhost/submit.php', 'r', false, $context);