Stream Contexts and POST Requests
Another concept introduced by streams is the context, which is basically a set of configuration options used in a streams operation. A context is created by passing an associative array of context options and their corresponding values to the stream_context_create function. One use of contexts with regard to the HTTP streams wrapper is making POST requests, as the wrapper uses the GET method by default.
<?php
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => implode("\r\n", array(
'Content-Type: application/x-www-form-urlencoded',
'Referer: http://localhost.example'
)),
'content' => http_build_query(array(
'paraml' => 'value1',
'param2' => 'value2'
))
)
));
$response = file_get_contents(
'http://localhost.example/process', false,
$context
);
?>
Here is a walk-through of this example.
© HTTP Streams Wrapper — Web Scraping >>> Back to TABLE OF CONTENTS <<< | |
| Views: 493 | |
| Total comments: 0 | |