PHP 使用socks5 代理和fsockopen ssl

问题描述 投票:0回答:2

我试图通过socks5代理使用php打开HTTPs页面,但我不能使用curl,因为我必须能够读取响应的每一行。

为此,我使用这个 PHP 类:https://web.archive.org/web/20150920143302/http://phpkode.com/source/s/socksed/socksed.php,但是如果我在端口 443 上启动连接,并发送带有参数的 HTTP POST 请求,nginx 向我发回此消息:

400 the plain http request was sent to https port

我尝试将这部分代码@fsockopen("tcp:// 更改为@fsockopen("ssl://,但随后我无法打开页面。

php curl ssl proxy socks
2个回答
0
投票

如果您可以使用curl,以下内容应该有助于实现您的目标:

// create the curl buffered reader object with a connection to a socks5 proxy on localhost port 8888
$cbr = new \CurlBufferedReader(array(
    CURLOPT_PROXY => 'socks5://127.0.0.1:8888',
    // disable ssl certificate verification, you probably don't need this so I've commented it out
    //CURLOPT_SSL_VERIFYPEER => false
));

// gets the body at the url up until the point specified by the callback function
$body = $cbr->get('https://stackoverflow.com/questions/24249029/php-use-socks5-proxy-with-fsockopen-ssl',
    /**
     * The callback function to determine whether to continue reading the body sent by the remote server
     * @param string $body
     */
    function (&$body) {
        if (($off = strpos($body, "\nTo do that,")) !== false) {
            // truncate body so that we have everything up until the newline before the string "To do that,"
            $body = substr($body, 0, $off + 1);
        }

        return $off === false; // continue reading while true
    });

echo $body;

以及相关类:

class CurlBufferedReader {
    private $body = false;
    private $callback;
    private $ch;

    public function __construct(array $curlopts = array()) {
        $this->ch = curl_init();

        curl_setopt_array($this->ch, $curlopts + array(
            // general curl options
            CURLOPT_CONNECTTIMEOUT   => 30,
            CURLOPT_FOLLOWLOCATION   => true,
            CURLOPT_MAXREDIRS        => 10,
            CURLOPT_RETURNTRANSFER   => true,

            // specific options to abort the connection early
            CURLOPT_BUFFERSIZE       => 8192,
            CURLOPT_WRITEFUNCTION    => array($this, 'writeFunction')
        ));
    }

    public function get($url, $callback = null) {
        curl_setopt($this->ch, CURLOPT_URL, $url);
        $this->callback = $callback;

        if (!($success = $this->exec())) {
            // errno 23 when aborted by the client
            if ($this->getErrno() !== 23) {
                $this->body = false;
            }
        }

        return $this->body;
    }

    public function getBody() {
        return $this->body;
    }

    public function getError() {
        return curl_error($this->ch);
    }

    public function getErrno() {
        return curl_errno($this->ch);
    }

    private function exec() {
        $status = curl_exec($this->ch);
        $this->callback = null;
        return $status;
    }

    private function writeFunction($ch, $buffer) {
        $this->body .= $buffer;

        if ($this->callback && !call_user_func_array($this->callback, array(&$this->body))) {
            $written = -1; // user callback requested abort
        } else {
            $written = strlen($buffer);
        }

        return $written;
    }
}

它的工作原理是从远程服务器一次读取 8,192 字节。调用用户函数来读取每次读取事件时收到的正文。当用户函数希望中止连接时,必须

return false
。同样重要的是要注意,
$body
是一个参考。因此,您对其所做的任何更改都会影响
CurlBufferedReader::get
方法接收到的字符串。


0
投票

试试这个:

$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
© www.soinside.com 2019 - 2024. All rights reserved.