Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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
如何使用file\u get\u内容在PHP中发布数据?_Php_Http_Http Post_File Get Contents - Fatal编程技术网

如何使用file\u get\u内容在PHP中发布数据?

如何使用file\u get\u内容在PHP中发布数据?,php,http,http-post,file-get-contents,Php,Http,Http Post,File Get Contents,我使用PHP函数file\u get\u contents()获取URL的内容,然后通过变量$http\u response\u header处理标题 现在的问题是,一些URL需要一些数据才能发布到URL(例如,登录页面) 我该怎么做 我意识到使用stream_上下文可能可以做到这一点,但我并不完全清楚 谢谢。使用发送HTTP POST请求其实并不难:正如您所猜测的,您必须使用$context参数 PHP手册中给出了一个例子,在本页:(引用): 基本上,您必须使用正确的选项创建流(该页面上有完

我使用PHP函数
file\u get\u contents()
获取URL的内容,然后通过变量
$http\u response\u header
处理标题

现在的问题是,一些URL需要一些数据才能发布到URL(例如,登录页面)

我该怎么做

我意识到使用stream_上下文可能可以做到这一点,但我并不完全清楚


谢谢。

使用发送HTTP POST请求其实并不难:正如您所猜测的,您必须使用
$context
参数


PHP手册中给出了一个例子,在本页:(引用):

基本上,您必须使用正确的选项创建流(该页面上有完整的列表),并将其用作
文件获取内容的第三个参数



作为旁注:一般来说,为了发送HTTP POST请求,我们倾向于使用curl,它提供了很多选项,但流是PHP的一个优点,没有人知道。。。太糟糕了…

另外,您也可以使用fopen

$params = array('http' => array(
    'method' => 'POST',
    'content' => 'toto=1&tata=2'
));

$ctx = stream_context_create($params);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if (!$fp)
{
    throw new Exception("Problem with $sUrl, $php_errormsg");
}

$response = @stream_get_contents($fp);
if ($response === false) 
{
    throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}

谢谢如果我需要将相同的POST参数传递到请求的页面,我猜我可以将$\u POST中的内容插入到$postdata中?我想你可以这样做;但是
content
不能是PHP数组:它必须是一个查询字符串(即,它必须具有以下格式:
param1=value1¶m2=value2¶m3=value3
);;这意味着您可能必须使用
http\u build\u query($\u POST)
太好了!我一直在寻找一种方法,通过执行
$postdata=http\u build\u query($\u POST)
可以将POST数据传递到另一个页面。有趣的是,这对我来说根本不起作用。我尝试了几个小时,所有的请求都变成了get QUERYST以发送多个标题值,使用
\r\n
换行符将它们全部放入一个字符串中-请参阅:这应该无限上移。如果你有原始的PHP功能,没有理由使用Curl/Guzzle或任何其他花哨的库。出于某种原因,这对我来说是可行的,但PHP官方示例没有+1也适用于
toto=1和tata=2
。然而,我没有使用
fopen
。@Iĺĺĺĺĺĺĺĺĺĺĺĺĺĺĺ。这是一个针对此类问题的友好警告。请尝试提供详细的答案,而不是简单地复制/粘贴代码。这也不必要地复杂。您可以使用
file\u get\u contents
而不是
fopen
+
stream\u get\u contents
。你甚至没有关闭“文件”。见@PascalMARTIN接受的答案。
$params = array('http' => array(
    'method' => 'POST',
    'content' => 'toto=1&tata=2'
));

$ctx = stream_context_create($params);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if (!$fp)
{
    throw new Exception("Problem with $sUrl, $php_errormsg");
}

$response = @stream_get_contents($fp);
if ($response === false) 
{
    throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}
$sUrl = 'http://www.linktopage.com/login/';
$params = array('http' => array(
    'method'  => 'POST',
    'content' => 'username=admin195&password=d123456789'
));

$ctx = stream_context_create($params);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if(!$fp) {
    throw new Exception("Problem with $sUrl, $php_errormsg");
}

$response = @stream_get_contents($fp);
if($response === false) {
    throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}