Why This Happens
cURL error 7 means the connection to the remote server was refused. This can happen because the server is down, the URL is wrong, DNS resolution fails, a firewall blocks outgoing connections, or the port is incorrect. This is a network-level issue, not a PHP code issue.
The Problem
$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// No error handlingThe Fix
$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
throw new RuntimeException("cURL error $errno: $error");
}
curl_close($ch);Step-by-Step Fix
- 1
Verify the URL
Check that the URL is correct, including the protocol (http vs https), domain name, and port number.
- 2
Test connectivity
Try to reach the server from the command line with curl or ping to determine if the issue is network-level.
- 3
Add error handling
Always check curl_exec() return value and use curl_error() and curl_errno() to get descriptive error information.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free