[英]PHP use socks5 proxy with fsockopen ssl
我正在尝试通过socks5代理使用php打开HTTPs页面,但是我不能使用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://)的那一部分,但是后来我无法打开页面。
如果您可以使用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
方法接收的字符串。
尝试这个:
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.