如何在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:
- We first use the first SOCKS5 proxy to send an HTTP
CONNECTrequest to the second proxy (loco-proxy.mx). This asks the first proxy to establish a direct tunnel to the second proxy. - If the tunnel is successfully created (we get a
200 Connection Establishedresponse), we then send our actual request tohttps://ifconfig.co/jsonthrough 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
CONNECTmethod 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 HTTPCONNECT. - Proxy Restrictions: If
sketchy-proxy.rublocks outgoing connections toloco-proxy.mx, this will fail. You’ll see errors like "Connection refused" or a non-200 HTTP status code. - Security: Never leave
CURLOPT_SSL_VERIFYPEERdisabled in production—it exposes you to man-in-the-middle attacks.
内容的提问来源于stack exchange,提问作者user11966347
相关产品推荐
相关产品推荐

