Signup/Sign In
OCTOBER 25, 2023

How to Send a POST request in PHP 7/8?

    If you use PHP and you want to send a POST request from your PHP backend to some API, then in this article I will share with you two different methods that you can use to send POST HTTP request with PHP.

    Generally, the frontend application fires most of the POST requests, or GET requests, to post some form of data to the backend or to get some data to display on the user interface, but there are many use cases where you may want to send a POST request from PHP backend.

    In this article, I will share with you 2 different ways to send POST request in PHP, and they are:

    1. cURL-less method (Using file_get_contents)

    2. Using cURL

    So let's get started.

    cURL-less method to send POST request from PHP

    First, let's see the CURL-less method, where we will be using the file_get_content() method, which is a part of the PHP's core library.

    If you have a default PHP installation you might not have cURL installed, because cURL is a PHP extension and may not exist in all the environments.

    Here is the code:

    $url = 'http://server.com/path';
    $data = ['key1' => 'value1', 'key2' => 'value2'];
    
    // use key 'http' even if you send the request to https://...
    $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);

    Let's try to decode the above code example,

    1. The $url variable will store the URL for the API where you want to send the POST request, and the $data variable will store the data that you want to send.

    2. Then in the $options, you can provide different header information that you may want to send along the POST request.

    3. Finally, you will use the stream_context_create() method to create a stream context, and then the file_get_contents() method to send the POST request.

    Using cURL to send POST request from PHP

    Now let's see how you can use cURL to send POST request in PHP.

    To use cURL, the cURL extension should be installed.

    Here is the code:

    <?php
    //The url you wish to send the POST request to
    $url = 'http://server.com/path';
    $data = ['key1' => 'value1', 'key2' => 'value2'];
    
    //The data you want to send via POST
    $fields = [
        'key1 '  => 'value1',
        'key2'   => 'value2',
        'key3'   => 'value3'
    ];
    
    // url-ify the data for the POST
    $fields_string = http_build_query($fields);
    
    // open connection
    $ch = curl_init();
    
    // set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    
    // So that curl_exec returns the contents of the cURL; rather than echoing it
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
    
    //execute post
    $result = curl_exec($ch);
    echo $result;
    ?>

    Change the $url in the code above to add your POST request URL, and also update the $fields variable to add the data fields that you want to send in the POST request.

    If you have cURL extension installed, then you should always use cURL, because this is a better option as compared to the file_get_contents() method.

    Conclusion

    While sending in POST request like this, make sure the header information is proper, the data that you are sending is properly validated if it is coming from the user, along with doing proper exception handling wherever required.

    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS