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 meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Send POST data with file_get_contents

Last accessed pages

  1. Simple Admin Login PHP Script (587)
  2. PuzzleImg - Script to Create Image Puzzle Game (1805)
  3. Editing, Changing XML - E4X (1002)
  4. Select in MySQL, Output results in HTML Table (696)
  5. Qwop (60365)

Popular pages this month

  1. Qwop (5845)
  2. Drag Racer V3 (2771)
  3. Flash Games - Free online Games (1439)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1246)
  5. Bubbles3 (1115)