好的,这是我需要的。我有一个基于PHP的网络爬虫。可在此处访问:http://rz7ocnxxu7ka6ncv.onion/现在,我的问题是,我的蜘蛛实际上需要在SOCKS端口9050上抓取页面。问题是,我必须通过Tor隧道连接,这样它才能解析.onion域,这就是我正在索引的。(只以.洋葱结尾。)我使用phpcrawl.php从命令行调用这个脚本,并添加适当的参数来爬网页面。以下是我的想法:有没有办法强迫它使用Tor?或者我可以强迫我的整个机器通过Tor进行隧道传输,以及如何?(就像强制所有流量通过127.0.0.1:9050一样)也许如果我设置了全局代理设置,php会尊重它们?
如果我的任何解决方案都有效,我该怎么做?(请按步骤说明,我是一个傻瓜。)
我只想装上我自己的Tor搜索引擎。(不要推荐我的p2p搜索引擎——这不是我想要的——我知道它们的存在,我做了功课。)如果你有兴趣看一看,下面是爬网程序的来源:也许有一个心地善良的人可以将其修改为对所有爬网请求使用127.0.0.1:9050?http://pastebin.com/kscGJCc5
cURL也支持SOCKS连接;试试这个:
<?php
$ch = curl_init('http://google.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
// SOCKS5
curl_setopt($ch, CURLOPT_PROXY, 'localhost:9050');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_exec($ch);
curl_close($ch);
除非我遗漏了什么,否则答案是肯定的,下面是Tor网站上的一些文档。说明书非常具体。虽然我还没有把托尔设为代理人,但这是我考虑过的事情,这是我要开始的地方。
编辑:正如文档所示,在Linux上设置Tor并将其用作代理是非常简单的。
sudo apt-get install tor
sudo /etc/init.d/tor start
netstat -ant | grep 9050 # verify Tor is running
现在,在查看了OP的代码之后,我们看到了对file_get_contents的调用。当您想开始参数化请求时,最初使用的最简单的方法变得很麻烦,因为您必须使用流上下文。
第一个建议是转向curl,但同样,更多地阅读SOCKS如何使用HTTP工作可能是为了真正回答这个问题。。。但要从技术上回答这个问题,如何将HTTP请求发送到本地主机上的Tor SOCKS代理,同样很容易。。
<?php
$ch = curl_init('http://google.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, 'https://127.0.01:9050/');
curl_exec($ch);
curl_close($ch);
但托尔告诉我们什么?
HTTP/1.0 501 Tor不是HTTP代理
内容类型:text/html;字符集=iso-8859-1
基本上,了解更多关于SOCKS&HTTP:。另一种选择是搜索PHP SOCKS客户端。快速检查会发现一个库声称可以通过SOCKS发送HTTP请求。
编辑:
好吧,再编辑一次!在完成上一篇文章几秒钟后,我找到了一种方法。这篇文章向我们展示了如何设置Privoxy,它将SOCKS请求转换为HTTP请求。把它放在Tor和blamo面前,我们通过Tor发送代理HTTP请求!
您必须通过使用"dnsport"指令配置tor来拦截来自php脚本的dns查找请求。然后您必须为tor配置一个"传输"和一个"虚拟网络地址"。现在,当php脚本通过tor进行dns查找时,tor会看到一个对洋葱地址的请求,并使用"virtualnetworkaddress"范围内的ip地址进行回答。现在,您必须将流向该地址的流量重定向到用"transport"定义的地址。阅读"torrc"手册中的"automaphostonresolve"、"virtualnetworkaddress"、"dnsport"answers"transport"。
我认为它就像使用usewithtor
或torify
选项运行命令行请求一样简单。例如:
$ usewithtor crawl.php
该脚本将能够与.onion网站进行交互。在为Tor自己构建了一个爬网程序后,我肯定不会走这条路用于生产,而是使用python、PySocks和其他爬网程序库,而不是CURL。希望这能回答你的问题,并为你未来的其他实施策略提供一些想法。
感谢
我搜索了如何在php中用Curl做同样的事情,我读了很多主题和例子,但这不起作用!没有成功我看到了另一个帖子:如何使用PHP中的cURL连接到Tor隐藏服务?关于Stackoverflow谁可能是有趣的
我已经成功地在PHP中找到了一个钩子,这对我来说很有效:
https://blockchainbdgpzk.onion/
的小例子
exec('curl -k --socks5-hostname 127.0.0.1:9150 "https://blockchainbdgpzk.onion/tobtc?currency=EUR&value=5"', $a);
print_r( $a );
return Array ( [0] => 0.0029577 )
由于我在Windows环境中,我已经将curl.exe和他的证书复制到文件夹c:''Windows''system32 中
或者类似的工作也只是添加这2个规则(-k)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
来源:PHP CURL CURLOPT_SSL_VERIFYPEER忽略
$url = "https://blockchainbdgpzk.onion/tobtc?currency=EUR&value=5";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_PROXYTYPE, 7 );
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt ($ch, CURLOPT_PROXY, '127.0.0.1:9150' );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$result = ob_get_contents();
ob_end_clean();
var_dump($result);
返回字符串"0.00296787"(长度=10)
它并不完美,但如果它能帮助别人的话。对不起我那些该死的英国朋友。
只需拥有HTTP代理:
<?php
/**
* Proxy script that performs any HTTP request requested.
*/
// Check key
$key = 'YOUR_API_KEY';
if($_GET['key'] != $key) die; // Check for the API key
// Check URL
$url = isset($_GET['url']) ? trim(base64_decode($_GET['url'])) : '';
if(!$url || !filter_var($url, FILTER_VALIDATE_URL)) die; // Incorrect URL
class MyCurl {
/**
* CURL resource link
*
* @var resource
*/
protected $resource;
/**
* Constructor
*
* @param String $host
* @return MyCurl
*/
public function __construct($url = 'localhost'){
$this->resource = curl_init();
$this->setUrl($url);
$this->setOptions(array(
// CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_REFERER => 'http://www.google.com/',
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_SSL_VERIFYPEER => FALSE,
));
}
/**
* Set URL for the next request
*
* @param String $url
*/
public function setUrl($url = 'localhost') {
$this->setOption(CURLOPT_URL, $url);
}
/**
* Sets option to the CURL resource.
* See http://www.php.net/manual/en/function.curl-setopt.php for option description
*
* @param int $name Option identifier
* @param mixed $value Option value
* @return Crawler_Curl Returns itself for sugar-code
*/
public function & setOption($name, $value){
curl_setopt($this->resource, $name, $value);
return $this;
}
/**
* Sets multiple CURL options at once
*
* @param array $options Associative array of options
* @return Crawler_Curl Returns itself for sugar-code
*/
public function & setOptions($options){
curl_setopt_array($this->resource, $options);
return $this;
}
/**
* Set User-Agent header of the browser
*
* @param String $useragent Defaults to Mozilla browser
*/
public function setUserAgent($useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0') {
$this->setOption(CURLOPT_USERAGENT, $useragent);
}
/**
* Get curl request info
*
* @array
*/
public function info() {
return curl_getinfo($this->resource);
}
/**
* Return sent headers if CURLINFO_HEADER_OUT option was enabled
*
* @return String Headers
*/
public function headersSent() {
return curl_getinfo($this->resource, CURLINFO_HEADER_OUT);
}
/**
* Executes CURL request
*
* @return mixed Returns CURL execution result
*/
public function execute(){
return curl_exec($this->resource);
}
/**
* Cleans CURL connection
*/
function __destruct(){
curl_close($this->resource);
}
}
$curl = new MyCurl($url);
$curl->execute();