【发布时间】:2020-06-20 07:48:55
【问题描述】:
我想将一个 PHP 页面重定向到另一个通过 HTTP POST 方法向其发送数据的页面。
我不能使用cURL,因为我需要相对的URLs(这个函数要从不同的位置调用),所以我在网上找到了这个函数:
function head($url)
{
$string = http_build_query($GLOBALS['new']);//$GLOBALS['new'] is an array containing some variables and I already tested it using var_dump()
$options = array('method'=>'POST', 'header'=>'Content-type: application/x-www-form-urlencoded', 'content'=>$string);
file_get_contents($url, false, stream_context_create(array('http'=>$options)));
echo error_get_last()['message'];//temporary to see the error message
header("Location: $url");
}
我会通过输入head(".?result=success) 或类似的东西来调用它...
问题是我收到此错误消息
file_get_contents(.?result=success): failed to open stream: No error
而目标页面没有收到任何东西。
不管它是什么意思,我找不到任何方法来修复它,甚至删除 HTTP GET 方法或改用绝对路径(即使它不是我的目的)也没有任何改变。
你能找到我的方法吗?
或者,如果不可能,您能否建议我另一种使用相对 URLs 发送 HTTP POST 的方法?
【问题讨论】:
-
head(".?result=success),你是说header("Location:?result=success")吗?不需要点 -
@dalelandry 输出相同(初始单点对于相对路径来说是可选的)
-
您是否包含了要发送到标头请求的页面?例如,如果我从索引页面发送标题重定向并检查结果是否设置在说联系页面上,我的标题重定向看起来像
header("Location: http://<--yoursite-->/contact.php?result=success") -
函数 file_get_contents 不能使用相对 URL,它必须是完整的 URL。但是您可以使用超级全局变量 $_SERVER 构建完整的 URL。
-
@dalelandry 我无法理解这与我的代码之间的区别......