admin管理员组

文章数量:1123407

I am trying to receive data from my API using socks5 proxy with curl and PHP.

But no matter what I try, I get this error Received invalid version in initial SOCKS5 response

I've tried google all I can, none come up with a fix. I tried all the solutions they list, but all returns this error.

This is my current code

<?php
function fetchContent($url) {
    $proxy = 'socks5h://domain:22';
    $proxyAuth = 'username:password';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

    $response = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    } else {
        echo $response;
    }

    curl_close($ch);
}

fetchContent('');
?>

As extra info, I am using same socks5 proxy over SSH on my laptop with putty tunnel to connect around and that works fine.

I am trying to receive data from my API using socks5 proxy with curl and PHP.

But no matter what I try, I get this error Received invalid version in initial SOCKS5 response

I've tried google all I can, none come up with a fix. I tried all the solutions they list, but all returns this error.

This is my current code

<?php
function fetchContent($url) {
    $proxy = 'socks5h://domain.com:22';
    $proxyAuth = 'username:password';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

    $response = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    } else {
        echo $response;
    }

    curl_close($ch);
}

fetchContent('https://ipinfo.io/ip');
?>

As extra info, I am using same socks5 proxy over SSH on my laptop with putty tunnel to connect around and that works fine.

Share Improve this question edited 15 hours ago HDIK asked 15 hours ago HDIKHDIK 531 silver badge6 bronze badges 2
  • 1 SOCKS5 proxies typically run on ports like 1080, not SSH ports like 22, check that first, and then If the proxy doesn't support the socks5h protocol (which resolves DNS over the proxy), you might need to use socks5 instead. – TSCAmerica.com Commented 13 hours ago
  • @TSCAmerica.com I have tried with socks5 too, it is still the same. About the port, I am trying to use sucks5 over the SSH protocol and the uses listens for SSH connection on that port. It works with the local tunnel on my putty which creates the tunnel over SSH, but I am not trying to create a tunnel with the script, just proxying the request thru the IP. Is it me that totally wrong here expecting that I can use the same method with curl? – HDIK Commented 13 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

By default, curl uses CURLPROXY_HTTP for CURLOPT_PROXYTYPE option.

To use socks5h you need to set CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME

<?php
# ... your code ...
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
# ... your code ...

P.S. Consider to remove protocol from proxy adddres definition

<?php  
$proxy = 'domain.com:22';

本文标签: Received invalid version in initial SOCKS5 response php curlStack Overflow