Home >Backend Development >PHP Tutorial >How Can I Send POST Requests in PHP Without Using cURL?
POST Request with PHP
For scenarios where a search query can only be performed using POST methods, understanding how to send POST requests with PHP is crucial. While GET methods are often insufficient for such situations, this article will guide you through the process of sending parameters via POST and retrieving the desired contents using PHP.
Using the CURL-Less Method
A simple yet effective way to send POST requests without using the CURL library is as follows:
$url = 'http://server.com/path'; $data = ['key1' => 'value1', 'key2' => 'value2']; // Use 'http' key even for HTTPS requests $options = [ 'http' => [ 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ], ]; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === false) { /* Handle error */ } var_dump($result);
Additional Resources
For further insights on this method, refer to the PHP manual linked below:
The above is the detailed content of How Can I Send POST Requests in PHP Without Using cURL?. For more information, please follow other related articles on the PHP Chinese website!