简体   繁体   English

将代理与PHP Simple HTML DOM解析器一起使用

[英]Using a proxy with PHP Simple HTML DOM Parser

I'm having some issues getting PHP Simple HTML DOM Parser to work with a proxy. 我在使PHP简单HTML DOM解析器与代理一起工作时遇到一些问题。 I read the info they have on the procedure in the manual but it still isn't cooperating. 我阅读了他们在手册中所描述的程序信息,但仍无法配合。

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>';

I only changed some parts, but clearly, the proxy example you provided it isn't working. 我只更改了一些部分,但是很明显,您提供的代理示例不起作用。 Try this instead: 尝试以下方法:

$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;

I managed to get it working using cURL to feed the page into PHP Simple HTML dom parser. 我设法使它使用cURL将页面输入到PHP Simple 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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM