【问题标题】:How to make a proxy stream through socks5 for PHP?如何通过 socks5 为 PHP 制作代理流?
【发布时间】:2016-08-21 23:18:33
【问题描述】:

我有这个代码:

$opt = array(
    'socks5' => array( 
        'proxy' => 'tcp://proxyIP:port', 
        'request_fulluri' => true,
    )
);

$stream = stream_context_create($opt);

if ($s = file_get_contents("http://yandex.ru/internet",FILE_USE_INCLUDE_PATH,$stream))
echo $s;

我猜它不起作用,因为获取站点会显示我的 IP 而不是代理 IP。

如果我输入 'http' 而不是 'socks5' 它可以工作并显示代理 IP。

在“http”的代理服务器中,端口 = 4000,“socks5”端口 = 5000

但我确实需要通过 SOCKS5 连接。我该怎么做?

【问题讨论】:

    标签: php proxy socks


    【解决方案1】:

    我认为让 stream_context_create() 与 socks5 一起工作是不可能的。最好的解决方案是使用 curl。示例根据你的需要,更改$proxyIP$proxyPort

    <?php
    
    //$opt = array('socks5' => array( 
    //                            'proxy' => 'tcp://proxyIP:port', 
    //                            'request_fulluri' => true,
    //                        )
    //            );
    //
    //$stream = stream_context_create($opt);
    //
    //if ($s = file_get_contents("http://yandex.ru/internet",FILE_USE_INCLUDE_PATH,$stream))
    //echo $s;
    
    $url = 'https://yandex.ru/internet';
    $proxyIP = '0.0.0.0';
    $proxyPort = 0;
    //$proxy_user = '';
    //$proxy_pass = '';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
    //curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxy_user}:{$proxy_pass}");
    curl_setopt($ch, CURLOPT_PROXY, $proxyIP);
    curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
    
    $s = curl_exec($ch);
    curl_close($ch);
    
    if ($s) {
        echo $s;
    }
    

    如果您不能使用 curl 或想使用套接字,请阅读此https://stackoverflow.com/a/31010287/3904683 答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多