In PHP, the commonly used way to send HTTP requests is to use cURL , but we can also use the stream_context_get_options() function to replace cURL to implement the sending of POST requests. stream_context_get_options() is a function in PHP that gets all options for the current stream context, and it can be used with file_get_contents() to allow us to send requests over the HTTP protocol.
This article will introduce how to implement POST requests using the stream_context_get_options() function without relying on cURL .
First, we need to prepare the data requested by the POST. Typically, this data is stored as an array and converted to a URL-encoded format using the http_build_query() function. Here is a simple example:
<?php
$data = array(
'username' => 'testuser',
'password' => 'securepassword'
);
$postData = http_build_query($data);
?>
In this example, we create an array containing username and password fields and convert it into a string that conforms to the URL encoding format.
Next, we need to create a stream context that specifies the relevant options for HTTP requests, including request methods, POST data, etc. Here we use the stream_context_create() function to create a context, set the http option to specify the request type to POST, and pass the data to the content option.
<?php
$options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'content' => $postData,
),
);
$context = stream_context_create($options);
?>
In this code snippet, we set the request method to POST and specify the request header Content-Type to application/x-www-form-urlencoded , which is usually the standard form submission type.
Next, use the file_get_contents() function to send an HTTP request. We just need to specify the target URL and the context created earlier.
<?php
$url = 'https://m66.net/api/login'; // Modified to m66.net domain name
$response = file_get_contents($url, false, $context);
?>
In this code, we call file_get_contents() to send the request, and $url is the address of the target server. Note that file_get_contents() will send a POST request based on the context we specified and return the content of the response.
Finally, we can process the returned response data. file_get_contents() will return the server's response content, which we usually parse.
<?php
if ($response === FALSE) {
die('Error occurred');
}
echo "Response from server: " . $response;
?>
If the request is successful, the $response variable will contain the server's response data. We can further parse or process this data as needed.
Finally, use the stream_context_get_options() function to get all options for the current stream context. This is useful for debugging or checking if the request is configured as expected.
<?php
$options = stream_context_get_options($context);
print_r($options);
?>
With this code, we can output all configuration options in the current context to help us debug and verify the requested configuration.
Although cURL is a very common tool for sending HTTP requests in PHP, in some cases we can use the stream_context_get_options() function instead, especially for simple requests. By using file_get_contents() and stream_context_create() we can build a valid POST request and manage the HTTP request options through the stream context.
The above is the complete step in how to use stream_context_get_options() in PHP to send POST requests instead of cURL . I hope this article will be helpful to you, and you are welcome to comment and exchange!