Source Code:
(back to article)
<?php // Set up the cURL request $ch = curl_init('https://jsonplaceholder.typicode.com/posts'); // Set the request method to POST curl_setopt($ch, CURLOPT_POST, true); // Set the POST data as a JSON string $data = [ 'title' => 'foo', 'body' => 'bar', 'userId' => 1, ]; $jsonData = json_encode($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); // Execute the request $response = curl_exec($ch); // Close the cURL handle curl_close($ch); // Do something with the response echo $response;
Result:
Report an issue