PHP使用socks5代理与fsockopen ssl

时间:2014-06-16 17:27:05

标签: php curl ssl proxy socks

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

要做到这一点,我正在使用这个PHP类:http://www.phpkode.com/source/s/socksed/socksed.php,但如果我在端口443上启动连接,并发送带有params的HTTP POST请求,nginx会将此消息发回给我:

400 the plain http request was sent to https port

我试图改变代码@fsockopen的部分(“tcp://到@fsockopen(”ssl://,但我无法打开页面。

2 个答案:

答案 0 :(得分: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方法收到的字符串。

答案 1 :(得分: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);