Skip to content

Instantly share code, notes, and snippets.

@daniloab
Created June 8, 2021 14:30
Show Gist options
  • Save daniloab/03de747c2e8469df8d81e6521527bd81 to your computer and use it in GitHub Desktop.
Save daniloab/03de747c2e8469df8d81e6521527bd81 to your computer and use it in GitHub Desktop.

run a http post request with PHP

  • create a new file with code below:
<?php
function httpPost($url, $data)
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Authorization' => 'someAuthorization',
        'x-api-key' => 'somekey',
        'Content-Type' => 'application/json'
    ]);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

      $response = httpPost('https://url', []);
echo $response;
  • go to the path of this file by terminal
  • run php yourfilename.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment