cURL Connection Error

cURL error 7: Failed to connect to api.example.com port 443: Connection refused

Quick Answer

PHP's cURL cannot reach the remote server. Check the URL, verify the server is running, check DNS resolution, and ensure no firewall is blocking the connection.

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 handling

The 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. 1

    Verify the URL

    Check that the URL is correct, including the protocol (http vs https), domain name, and port number.

  2. 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. 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