Home > Backend Development > PHP Tutorial > How Can I Send POST Requests in PHP Without Using cURL?

How Can I Send POST Requests in PHP Without Using cURL?

Patricia Arquette
Release: 2024-12-26 12:38:10
Original
856 people have browsed it

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);
Copy after login

Additional Resources

For further insights on this method, refer to the PHP manual linked below:

  • stream_context_create: https://www.php.net/manual/en/function.stream-context-create.php

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template