Php-mysql Course

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

Click on the tag that creates a cell in table
<tr> <span> <td>
Indicate the CSS property used to specify a background image for an element
background-color background-image color
h3 {
  background-image: url("image.jpg");
What instruction can be used to parse all the array items?
for() [) object()
var arr = [1, "ab", ""];
for(var i=0; i< arr.length; i++) { alert(arr[i]); };
Indicate the PHP instruction used to traverse an associative array.
for() foreach() if()
$arr =["k1"=>"v1", "k2"=>"v2", "k3"=>"v3");
foreach($arr AS $k => $v) { echo "<br/>". $k ." - ". $v; }
Send POST data with file_get_contents

Last accessed pages

  1. Display data from PHP Array, or MySQL in HTML table (19479)
  2. Add and Remove HTML elements and Content with jQuery (27498)
  3. Ajax-PHP Chat Script (44525)
  4. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (21531)
  5. Display UL bullets and OL numbers on the right side (3694)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (812)
  2. querySelector and querySelectorAll (438)
  3. Read Excel file data in PHP - PhpExcelReader (404)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (380)
  5. JavaScript Course - Free lessons (293)