338

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.

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

How do I do that?

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

Thanks.

1

3 Answers 3

662
Answer recommended by PHP Collective

Sending an HTTP POST request using file_get_contents is not that hard, actually : as you guessed, you have to use the $context parameter.


There's an example given in the PHP manual, at this page : [HTTP context options][2] *(quoting)* :
$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 ;-)


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...
12
  • 1
    Thanks. I am guessing I can insert the contents from $_POST into $postdata if I need to pass same POST params to the requested page? Mar 15, 2010 at 6:49
  • 7
    I suppose you can do something like that ; but content must not be a PHP array : it has to be a querystring (i.e. it must has this format : param1=value1&param2=value2&param3=value3 ) ;; which means you'll probably have to use http_build_query($_POST) Mar 15, 2010 at 6:52
  • 3
    Wonderful! I was looking for a way to pass POST data to another page which is achievable by doing $postdata = http_build_query($_POST). Nov 30, 2011 at 12:20
  • 1
    intresting enough this does not work for me at all i been tryiung it for a few hours and all my requets get turned into get querys
    – WojonsTech
    Oct 2, 2012 at 7:01
  • 1
    To send multiple header values, throw them all into one string with \r\n line breaks - see: stackoverflow.com/a/2107792/404960
    – rymo
    Jul 3, 2014 at 16:08
24

An alternative, you can also use 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");
}
2
  • For some reason, this worked for me, but the PHP official example did not. +1 for the toto=1&tata=2 as well. I didn't use the fopen, however. Jul 17, 2015 at 19:09
  • 4
    @Ġiĺàɗ We don't call people 'noob' here. This is a friendly warning against such.
    – Daedalus
    Jan 26, 2016 at 10:35
-2
$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");
}
2
  • 2
    Please, try to provide an elaborated answer instead of simply copying/pasting code. Apr 6, 2018 at 18:18
  • 1
    Also this is unnecessarily complicated. You can use file_get_contents instead of fopen + stream_get_contents. And you are not even closing the "file". See the accepted answer by @PascalMARTIN. Sep 21, 2018 at 11:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.