HomePhpPHP sample curl PHP sample curl Makarablue January 19, 2023 0 Example of how to use cURL in PHP to make a GET/POST request // The URL to make the request to $url = 'https://www.example.com'; // Initialize cURL $ch = curl_init(); // Set the URL curl_setopt($ch, CURLOPT_URL, $url); // Set the method to GET curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Return the response curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute the request $response = curl_exec($ch); // Close the cURL session curl_close($ch); // Print the response echo $response;You can also use cURL to make POST requests by setting the method to POST and including the data to be sent in the request.// The URL to make the request to $url = 'https://www.example.com'; // Data to be sent in the request $data = array( 'param1' => 'value1', 'param2' => 'value2', ); // Initialize cURL $ch = curl_init(); // Set the URL curl_setopt($ch, CURLOPT_URL, $url); // Set the method to POST curl_setopt($ch, CURLOPT_POST, 1); // Set the data to be sent curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Return the response curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute the request $response = curl_exec($ch); // Close the cURL session curl_close($ch); // Print the response echo $response; Tags Coding Php Newer Older