Search code examples
phpphp-curl

Make POST request from php file to another php file


I am trying to send a post request that does the excact same thing as the code below, but from a php file to another php file. In other words I want to make the post request with PHP and not Javascript. I have tried different methods but they all get the content from the php file, and not make the post like ajax.

$.ajax({
    url: "./delete.php",
    method: "POST",
    data: {
        id: 1
    },
    success: data => console.log(data),
    error: err => console.log(err)
})

Solution

  • This is the code sample I used. Thanks to @Amirhossein Shahbazi for the suggestion. Link to the question: send http request with CURL to local file

    <?php
    $url = 'http://localhost:8000/fe.php';
    // The submitted form data, encoded as query-string-style
    // name-value pairs
    $body = 'monkey=uncle&rhino=aunt';
    $c = curl_init ($url);
    curl_setopt ($c, CURLOPT_POST, true);
    curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
    curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
    $page = curl_exec ($c);
    curl_close ($c);
    ?>