// Initialize cURL session $ch = curl_init(); // Set request URL curl_setopt($ch, CURLOPT_URL, "https://example.com/api/users"); // Set request method to GET curl_setopt($ch, CURLOPT_HTTPGET, true); // Execute cURL session $result = curl_exec($ch); // Check for errors if(curl_errno($ch)) { echo 'Error: ' . curl_error($ch); } // Close cURL session curl_close($ch); // Display the response echo $result;
// Import GuzzleHTTP library require __DIR__ . '/vendor/autoload.php'; use GuzzleHttp\Client; // Initialize GuzzleHTTP client $client = new Client(['base_uri' => 'https://example.com/api/']); // Send POST request with data $response = $client->post('users', [ 'form_params' => [ 'name' => 'john', 'email' => 'john@example.com', 'password' => 'password', ], ]); // Display the response echo $response->getBody();In example 2, the GuzzleHTTP package is used to send a POST request with data to the API endpoint. The package provides a convenient and easy-to-use API for sending HTTP requests in PHP.