= 200 && $httpCode < 300) { return json_decode($response, true); } else { error_log("API call to {$url} failed with HTTP code {$httpCode}. Response: {$response}"); return null; } } // Example Usage: // $apiUrl = "https://api.example.com/create-resource"; // Replace with your API endpoint // $postData = [ // 'name' => 'New Item', // 'description' => 'This is a description for the new item.', // 'quantity' => 10 // ]; // $customHeaders = [ // 'X-Custom-Header: MyValue', // 'Authorization: Bearer YOUR_ACTUAL_API_KEY' // Replace with your actual key // ]; // $result = postJsonToApi($apiUrl, $postData, $customHeaders); // if ($result) { // echo "Resource created successfully: "; // print_r($result); // } else { // echo "Failed to create resource. "; // } // You can test with a public API like httpbin.org for POST requests: // $testApiUrl = "https://httpbin.org/post"; // $testData = ['message' => 'Hello from PHP cURL', 'timestamp' => time()]; // $testResult = postJsonToApi($testApiUrl, $testData); // if ($testResult) { // echo " Test API call result: "; // print_r($testResult); // } ?>