To make a simple HTTP GET request using cURL in PHP, you can follow these steps:
Step 1: Initialize cURL session using curl_init()
.
$curl = curl_init();
Step 2: Set the URL you want to make the GET request to using curl_setopt()
with the option CURLOPT_URL
.
curl_setopt($curl, CURLOPT_URL, 'http://example.com');
Step 3: Specify that you want to receive the response as a string by setting CURLOPT_RETURNTRANSFER
option to true
.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Step 4: Execute the request using curl_exec()
.
$response = curl_exec($curl);
Step 5: Close the cURL session using curl_close()
.
curl_close($curl);
Here's the complete code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://example.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
After executing these steps, the response from the HTTP GET request can be accessed through the $response
variable.