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 request body data as JSON $data = [ 'title' => 'foo', 'body' => 'bar', 'userId' => 1, ]; $data_json = json_encode($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); // Set the request headers to indicate that JSON data is being sent curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: ' . strlen($data_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