Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将帖子发送到URL_Php_Http_Url_Post_Send - Fatal编程技术网

Php 将帖子发送到URL

Php 将帖子发送到URL,php,http,url,post,send,Php,Http,Url,Post,Send,我想将一个PHP页面重定向到另一个通过HTTP POST方法发送数据的页面 我不能使用cURL,因为我需要相对URL(该函数将从不同位置调用),所以我在线找到了该函数: 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(

我想将一个PHP页面重定向到另一个通过HTTP POST方法发送数据的页面

我不能使用cURL,因为我需要相对URL(该函数将从不同位置调用),所以我在线找到了该函数:

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\u get\u contents(.?result=success):无法打开流:无错误

并且目标页面没有收到任何内容


不管它意味着什么,我找不到任何方法来修复它,甚至删除HTTP GET方法或放置绝对路径(即使这不是我的初衷)都不会改变

你能帮我找到一个方法让它工作吗?


或者,如果不可能,您可以建议我使用相对URL发送HTTP POST的另一种方法吗?

这是我在我的站点用户类中用于登录的代码的一个工作示例

$url = 'http://' . $_SERVER['HTTP_HOST'];            // Get the server
$url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
$url .= 'index.php?error';            // <-- Your relative path
header('Location: ' . $url, true, 302);
exit;
$url='http://'。$\u SERVER['http\u HOST'];//获取服务器
$url.=rtrim(dirname($\u SERVER['PHP\u SELF']),'/\\\');//获取当前目录

$url.='index.php?error';//
头(“.?result=success)
,您是指
头(“位置:?result=success”)
?不应该有圆点needed@dalelandry输出是相同的(起始处的初始单点对于相对路径是可选的)您是否包含要发送到标头请求的页面?例如,如果我从索引页发送标题重定向,并检查是否在say contact page上设置了结果,则我的标题重定向看起来像
标题(“位置:http:///contact.php?result=success“”
函数文件“获取”内容不能与相对URL一起使用,它必须是完整的URL。但是您可以使用超级全局var$\u服务器构建完整的URL。@Daelandry我无法理解这与我的代码之间的区别……302是什么意思?
文件获取内容
仍然不发送HTTP POST data302是一个响应状态码。。。Mozilla MDN:带有此状态代码的HTTP响应将在标题字段位置中另外提供一个URL。这是对用户代理(例如web浏览器)的邀请,请其对位置字段中指定的新URL发出第二个相同的请求。最终的结果是重定向到新的URL。那么将帖子发送到该URL如何?您的帖子是如何生成的?这是一篇来自表单还是其他来源的帖子?您是否希望动态创建header()重定向,然后将POST变量应用于该URL?