It is possible to send POST data with file_get_contents() and get the returned content, by using it with the stream_context_create() function.
The stream_context_create($opt) creates and returns a stream context with any options supplied in the $opt array.
- $opt is an array of associative arrays in the format: $arr['wrapper']['option'] = $value .

Example stream_context_create() and file_get_contents()

- Click on the code to select it.
$postdata = http_build_query(['name'=>'some name', 'pass'=>'password']);  //data to send 'name=some+name&pass=password'
$opts = [
 'http' =>
   'method'=> 'POST',  // GET or POST
   'header' => 'Content-type: application/x-www-form-urlencoded'. PHP_EOL .'Content-Length: '. strlen($postdata). PHP_EOL,
   'content' => $postdata
$send_data  = stream_context_create($opts);
$cnt = file_get_contents('', false, $send_data);  //send data and get the returned content

echo $cnt;
- The http_build_query($arr) function generates a URL-encoded query string from the $arr, returns a string like this: 'key1=val1&key2=val2'.
- The value of the key 'method' must be in UPPERCASE (GET or POST).
- The 'header' key contains http headers that must be send to "page.php".

Daily Test with Code Example

Which tag adds an image in web page?
<div> <img> <span>
<img src="" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
Send POST data with file_get_contents

Last accessed pages

  1. Get and Modify content of an Iframe (15658)
  2. Contact page - CoursesWeb (5492)
  3. Tabs effect with CSS (6210)
  4. Output or Force Download MP3 with PHP (1621)
  5. Insert, Select and Update NULL value in MySQL (35121)

Popular pages this month

  1. Tabs effect with CSS (2293)
  2. Contact page - CoursesWeb (2285)
  3. Insert, Select and Update NULL value in MySQL (1440)
  4. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1020)
  5. PHP getElementById and getElementsByTagName (790)