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('http://domain.net/page.php', 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
HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li><ul>
<li>http://coursesweb.net/html/</li>
<li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block.some_class {
display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()var obj = {
"courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr); // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue; // CoursesWeb.net