What is the usage method of php curl_exec?
To execute a cURL session in PHP, use the curl_exec function. Here is a basic method for using the curl_exec function:
- Create a cURL handle.
$ch = curl_init();
- Configure cURL options, such as setting the URL, request method, and request headers.
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- Execute the cURL session and retrieve the response data.
$response = curl_exec($ch);
- Check for any errors and retrieve error information.
if(curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
}
- Close the cURL session.
curl_close($ch);
Before using the curl_exec function, ensure that all necessary cURL options have been set correctly and the cURL handle has been initialized. The curl_exec function will execute the cURL request and return the response data, which can be processed as needed.