2014-07-21 148 views
2

我在使用代理时遇到一些问题,PHP Simple HTML DOM Parser。我阅读了他们对手册中程序的信息,但它仍然不合作。在PHP中使用代理简单的HTML DOM解析器

require_once('simple_html_dom.php'); 

$url = 'http://www.whatsmyip.org/'; 
$proxy = '00.000.000.80:80'; 

$context = array( 
    'http' => array( 
     'proxy' => $proxy, 
     'request_fulluri' => true, 
    ), 
); 
$context = stream_context_create($context); 

$dom = new simple_html_dom(); 
$dom = file_get_html($url, false, $context); 

echo '<pre>'; 
print_r($dom); 
echo '</pre>'; 
+0

啊是的。 “不工作”。非常具体。你有没有尝试通用电脑治愈#1:重新启动/重新启动? –

+0

你确实拥有'00.000.000.80:80'的代理服务器吗?因为这不是一个有效的IP。 –

+0

@MarcB对不起Mark,看起来会在一分钟后超时。 –

回答

2

我只改变了一些部分,但很明显,您提供的代理示例不起作用。试试这个:

$context = array('http' => array('proxy' => 'tcp://221.176.14.72:80','request_fulluri' => true,),); 
$stream = stream_context_create($context); 
$dom = file_get_html('http://www.whatsmyip.org/', false, $stream); 
$ip = $dom->find('span#ip', 0)->innertext; 
echo $ip; 
+0

这对我有效,谢谢。我试过的代理人可能不好想出来。我有一个替代解决方案,以下工作。 –

2

我设法让它使用cURL来将页面提供给PHP简单的HTML dom解析器。

require_once('simple_html_dom.php'); 

$url = 'http://www.whatsmyip.org/'; 
$proxy = '00.000.000.80:80'; 

$options = array( 
    CURLOPT_PROXY   => $proxy, 
    CURLOPT_HTTPPROXYTUNNEL => 0, 
    CURLOPT_REFERER  => "http://www.google.com", 
    CURLOPT_FOLLOWLOCATION => true, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_USERAGENT  => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1", 
    CURLOPT_CONNECTTIMEOUT => 20, 
    CURLOPT_TIMEOUT  => 20, 
    CURLOPT_MAXREDIRS  => 10, 
    CURLOPT_HEADER   => true, 

); 

$ch = curl_init($url); 
curl_setopt_array($ch, $options); 
$content = curl_exec($ch); 

$dom = new simple_html_dom(); 
$dom->load($content,true,false); 

echo '<pre>'; 
print_r($dom); 
echo '</pre>'; 
+1

以及使用它也是合乎逻辑的,因为卷曲非常灵活 – Ghost

相关问题