How can you check if a cURL request was successful in PHP?
To check if a cURL request was successful in PHP, you can use the `curl_getinfo()` function to get information about the request, specifically the HTTP response code. A successful request typically returns a response code of 200.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) {
echo 'cURL request was successful';
} else {
echo 'cURL request failed';
}
curl_close($ch);
Keywords
Related Questions
- In what scenarios would it be advisable to use fopen, fsockopen, or cURL instead of file_get_contents in PHP?
- How can PHP beginners ensure that no output is generated before using the header() function for URL redirection?
- In the context of adding a plus sign before a number for human readability, what are the best practices in PHP?