php 如何使用file_get_contents在PHP中发布数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2445276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:30:19  来源:igfitidea点击:

How to post data in PHP using file_get_contents?

phphttphttp-postfile-get-contents

提问by Paras Chopra

I'm using PHP's function file_get_contents()to fetch contents of a URL and then I process headers through the variable $http_response_header.

我正在使用 PHP 的函数file_get_contents()来获取 URL 的内容,然后我通过变量处理标头$http_response_header

Now the problem is that some of the URLs need some data to be posted to the URL (for example, login pages).

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

How do I do that?

我怎么做?

I realize using stream_context I may be able to do that but I am not entirely clear.

我意识到使用 stream_context 我也许能够做到这一点,但我并不完全清楚。

Thanks.

谢谢。

回答by Pascal MARTIN

Sending an HTTP POST request using file_get_contentsis not that hard, actually : as you guessed, you have to use the $contextparameter.

使用发送 HTTP POST 请求file_get_contents并不难,实际上:正如您猜测的那样,您必须使用$context参数。


There's an example given in the PHP manual, at this page : HTTP context options(quoting):


PHP 手册中给出了一个示例,在此页面上:HTTP 上下文选项(引用)

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

Basically, you have to create a stream, with the right options (there is a full list on that page), and use it as the third parameter to file_get_contents-- nothing more ;-)

基本上,您必须使用正确的选项创建一个流(该页面上有一个完整列表),并将其用作第三个参数file_get_contents- 仅此而已;-)


As a sidenote : generally speaking, to send HTTP POST requests, we tend to use curl, which provides a lot of options an all -- but streams are one of the nice things of PHP that nobody knows about... too bad...


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

回答by Macbric

An alternative, you can also use fopen

另一种选择,您也可以使用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");
}

回答by user2525449

$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");
}