HTTP POST Data Transmission using PHP's file_get_contents()
When attempting to fetch URL content using PHP's file_get_contents() function, specific URLs may necessitate data posting. To fulfill this need, one can utilize the stream_context parameter.
To construct the necessary stream context, employ the following steps:
Example Code:
$postData = http_build_query([ 'var1' => 'some content', 'var2' => 'doh' ]); $opts = ['http' => ['method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => $postData] ]; $context = stream_context_create($opts); $result = file_get_contents('http://example.com/submit.php', false, $context);
By implementing these steps, you can effectively submit HTTP POST data using file_get_contents(). While curl offers more capabilities, PHP's streams provide a less well-known but equally powerful alternative for HTTP POST operations.
The above is the detailed content of How Can I Send HTTP POST Data Using PHP's `file_get_contents()`?. For more information, please follow other related articles on the PHP Chinese website!