Skip to content

Instantly share code, notes, and snippets.

@paulgrammer
Created May 25, 2022 13:18
Show Gist options
  • Save paulgrammer/ebd552062cd831754e426c9800b9748d to your computer and use it in GitHub Desktop.
Save paulgrammer/ebd552062cd831754e426c9800b9748d to your computer and use it in GitHub Desktop.
<?php
// API URL
$url = 'http://localhost/post-path';
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'name' => 'test'
);
$payload = json_encode($data);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
// log result
var_dump($result);
// Close cURL resource
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment