【问题标题】:Using a proxy with PHP Simple HTML DOM Parser将代理与 PHP Simple HTML DOM Parser 一起使用
【发布时间】:2014-07-21 22:14:44
【问题描述】:

我在让PHP Simple HTML DOM Parser 使用代理时遇到了一些问题。我阅读了他们在手册中有关程序的信息,但仍然不合作。

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

【问题讨论】:

  • 啊,是的。 “不工作”。很具体。您是否尝试过 Universal Computer Cure #1:重启/重启?
  • 您实际上在00.000.000.80:80 有代理服务器吗?因为那不是一个有效的 IP。
  • @MarcB 抱歉,马克,一分钟后它似乎超时了。
  • @JonathanKuhn,不,我只是想为 StackOverflow 隐藏它。

标签: php html web-scraping simple-html-dom


【解决方案1】:

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

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

【讨论】:

  • 这对我有用,谢谢。我尝试过的代理可能很糟糕。我在下面有一个替代解决方案也可以。
【解决方案2】:

我设法使用 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>';

【讨论】:

  • 好吧,使用它也是合乎逻辑的,因为 curl 非常灵活
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2010-12-29
  • 1970-01-01
  • 2012-08-12
  • 2012-04-08
  • 2017-06-08
相关资源
最近更新 更多