You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP的(lib)cURL中使用双重代理实现双跳连接?

Can I chain two proxies in PHP cURL to get a Mexico IP?

Absolutely, this is feasible—though it depends entirely on whether your first proxy (sketchy-proxy.ru) allows forwarding traffic to a second proxy. Assuming it’s an open proxy that permits this kind of chaining, here’s how to pull off the double-hop connection in PHP with cURL:

How it works

cURL doesn’t have a built-in "multi-proxy" setting, but we can manually create a tunnel through the first proxy to the second one. Here’s the breakdown:

  1. We first use the first SOCKS5 proxy to send an HTTP CONNECT request to the second proxy (loco-proxy.mx). This asks the first proxy to establish a direct tunnel to the second proxy.
  2. If the tunnel is successfully created (we get a 200 Connection Established response), we then send our actual request to https://ifconfig.co/json through this existing tunnel.

Working PHP Code

Make sure to replace loco-proxy.mx:YOUR_PORT with the actual port of your second proxy (common ports are 8080 for HTTP, 1080 for SOCKS5):

$ch = curl_init();
$targetUrl = 'https://ifconfig.co/json';
$secondProxy = 'loco-proxy.mx:YOUR_PORT'; // Replace with actual port

// Step 1: Configure cURL to use the first SOCKS5 proxy
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
curl_setopt($ch, CURLOPT_PROXY, 'sketchy-proxy.ru:446');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Only for testing—disable in production

// Step 2: Ask the first proxy to create a tunnel to the second proxy
$targetHost = parse_url($targetUrl, PHP_URL_HOST);
$targetPort = parse_url($targetUrl, PHP_URL_PORT) ?: 443; // Default to 443 for HTTPS
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($ch, CURLOPT_URL, "http://{$secondProxy}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "CONNECT {$targetHost}:{$targetPort} HTTP/1.1");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Host: {$targetHost}",
    "Proxy-Connection: keep-alive"
]);

// Execute tunnel creation request
$tunnelResponse = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($httpCode === 200) {
    // Step 3: Tunnel is ready—send request to target through the tunnel
    curl_setopt($ch, CURLOPT_URL, $targetUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, null); // Reset to GET
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "User-Agent: cURL, used through PHP",
        "Accept: application/json"
    ]);

    $output = curl_exec($ch);
    var_dump($output);
} else {
    echo "Failed to create tunnel: " . curl_error($ch) . " (HTTP status: {$httpCode})";
}

curl_close($ch);

Key Notes

  • Proxy Compatibility: This example assumes your second proxy is an HTTP proxy. If it’s a SOCKS5 proxy, the CONNECT method won’t work—you’ll need a proxy that supports SOCKS5-to-SOCKS5 forwarding, and you’d have to adjust the code to use SOCKS5 commands instead of HTTP CONNECT.
  • Proxy Restrictions: If sketchy-proxy.ru blocks outgoing connections to loco-proxy.mx, this will fail. You’ll see errors like "Connection refused" or a non-200 HTTP status code.
  • Security: Never leave CURLOPT_SSL_VERIFYPEER disabled in production—it exposes you to man-in-the-middle attacks.

内容的提问来源于stack exchange,提问作者user11966347

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:26:13