How to use a SOCKS 5 proxy with cURL?

How to use a SOCKS 5 proxy with cURL?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

SOCKS5 Authentication in cURL: Beyond the "1" Error

You can absolutely use a SOCKS5 proxy with cURL, but the implementation differs significantly from handling standard HTTP proxies. The error [1 appearing on your page is not a random glitch; it is the direct result of how you are passing credentials to a protocol that expects them in a specific format.

In your current setup, you are attempting to pass the username and password as part of the proxy string itself:
$proxy = "cagsan:jw22wdw@108.61.25.223:34792";

This works for HTTP proxies because many servers parse the user:pass@host syntax directly from the configuration string. However, SOCKS5 operates on a different handshake mechanism. It does not natively support embedding credentials in the connection URL or the initial proxy options like CURLOPT_PROXY. When you attempt to connect, the SOCKS server accepts the IP and port but rejects the connection immediately because no authentication has been performed yet. The server then returns the error code 1, which translates to ECONNREFUSED (Connection Refused) in cURL's output.

To resolve this, you must detach the credentials from the proxy string and pass them explicitly using the CURLOPT_PROXYUSERPWD option. This forces cURL to include the SOCKS5 authentication header during the handshake process before establishing the tunnel.

Here is the corrected approach for PHP:

$proxyHost = '108.61.25.223';
$proxyPort = 34792;
$username = 'cagsan';
$password = 'jw22wdw';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
// Explicitly set the authentication credentials
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);

$curl_scraped_page = curl_exec($ch);
curl_close($ch);

By separating the host and port from the credentials, you ensure that cURL constructs the correct SOCKS5 handshake packet. If the server requires authentication, it will now receive the username and password correctly, allowing the tunnel to establish successfully.

Handling SOCKS5 with Passwords via URL

While the explicit option is the cleanest method in code, you might also consider how these credentials interact with the target URL if you are using other tools. In command-line scenarios or different languages, some libraries allow passing credentials via the URL scheme (e.g., http://user:pass@host). However, this syntax is unreliable for SOCKS5 because the proxy server interprets the string differently depending on its implementation. Relying on the explicit CURLOPT_PROXYUSERPWD option is the robust standard for production scripts.

The Trade-off of Connection Refused

The distinction between HTTP and SOCKS5 proxies is critical here. With an HTTP proxy like 72.41.132.22:3128, the server often acts as a forwarder immediately upon seeing the request line, making simple string parsing sufficient for authentication. SOCKS5 requires a dedicated negotiation phase. If you fail to provide valid credentials in this specific way, the connection is refused instantly. You cannot "retry" with different parameters on the same connection; cURL must close and reopen the session with the correct configuration every time.

For developers working with complex scraping stacks or automation scripts, ensuring the proxy string format matches the protocol requirements is the single most common point of failure. If you are using a framework like Laravel to handle these requests, you would typically configure this via the curl_setopt array within your service layer, passing the authenticated proxy string as shown above rather than trying to embed it in the base URL.

If you require further assistance with building robust proxy handling services or need guidance on configuring similar networking layers within a Laravel ecosystem, you can explore resources at https://laravelcompany.com.

Our network