【问题标题】:simplexml patch for allow_url_fopen to be use on simplehtmldom在 simplehtmldom 上使用 allow_url_fopen 的 simplexml 补丁
【发布时间】:2011-08-19 11:04:18
【问题描述】:

我的托管公司已禁用 allow_url_fopen 和 allow_url_include。他们建议我改用 cURL,然后我在 simplexml 上看到了这个补丁

$calendar = simplexml_load_file($source); with
if (ini_get('allow_url_fopen')) {
$calendar =       simplexml_load_file($source); 
}   
else {    
$ch = curl_init($source);    
curl_setopt  ($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
$xml_raw = curl_exec($ch);    
$calendar = simplexml_load_string($xml_raw);  }

它适用于我的主机,所以我想知道是否可以在 simplehtmldom 上使用它这是我正在使用的代码

<?php
include('simple_html_dom.php');
include ('phpQuery.php');   
// Create DOM from URL
$html = file_get_html('http://www.urlhere.com/');
   ?>

我可以用这个吗?如果是,我应该进行哪些更改,谢谢您的帮助

修改了上面的代码

<?php
include('simple_html_dom.php'); 
include ('phpQuery.php'); 

if (ini_get('allow_url_fopen')) {       
$html = file_get_html('http://www.weather.bm/'); }
else{       $ch = curl_init('http://www.weather.bm/');           
curl_setopt  ($ch, CURLOPT_HEADER, false);        
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);           
$src = curl_exec($ch);           
$html = str_get_html($src, false); 
var_dump($html);  }   

?>

<div id="leftwrapper">
<div id="CurrentConditions">
<h2>Current Conditions</h2>

<div id="current-content">
<div id="elementleft">
<?php
foreach($html->find('div.obElementLeft') as $e)
echo $e->outertext;
?>
</div>
<div id="elementright"><?php 
foreach($html->find('div.obElementRight') as $e)
echo $e->outertext; 
?>
</div></div></div>
<div id="rightwrapper"> 
<div id="TodayForecast">
<h2>24-Hour Forecast</h2>
<?php 
foreach($html->find('.TodaysForecastContainer') as $e)
$doc = phpQuery::newDocumentHTML( $e );  
$containers = pq('.TodaysForecastContainerInner', $doc); 
foreach( $containers as $container ) {     
$div = pq('div', $container);      
$div->eq(1)->removeAttr('style')->addClass('thumbnail')->
html( pq( 'img', $div->eq(1))->removeAttr('height')->
removeAttr('width')->removeAttr('alt') ); 
$div->eq(0)->removeAttr('style')->addClass('day')-> 
html( pq( 'u', $div->eq(0) )->html() );           
$div->eq(2)->removeAttr('style')->addClass('description');   }  
print $doc; 
?>

</div><!--end of todayforecast--> 
<div id="bws-foot">Weather data provided in part by the Bermuda Weather
Service</div>     <img id="bws-logo" src="images/bwslogo.jpg" />
<div id="hilo">
<p>Today's Temperature:</p>
<?php
foreach($html->find('div.HiLoContainer') as $e)
$doc = phpQuery::newDocumentHTML( $e );  
$containers = pq('div.HiLoContainer', $doc); 
foreach( $containers as $container ) {     $div = pq('div', $container);        
$div->eq(0)->removeAttr('style')->addClass('hi1');  
$div->eq(1)->removeAttr('style')->addClass('lo1'); 
}  print $doc; 
?>

展望

find('.SynopsisContainer span') as $e) 回声 $e->innertext 。 '
'; ?>

4 天天气预报

foreach($html->find('.FourDayForecastContainer') as $e)

$doc = phpQuery::newDocumentHTML($e);
$containers = pq ('.FourDayForecastContainerInner', $doc); foreach( $containers as $container ) { $div = pq('span', $container);
$img = pq('img', $container);
$div->eq(0)->removeAttr('style')->addClass('day')-> html( pq( 'u', $div->eq(0) )->html() );

$img->eq(0)->removeAttr('style')->removeAttr('height')-> removeAttr('width')->removeAttr('alt')->addClass('thumbnail')-> html( pq( 'img', $img->eq(0)) );$imghtml = pq('a', $container)-> html(); pq($container)->prepend($imghtml); pq('a', $container)->remove();
$div->eq(1)->removeAttr('style')->addClass('hi');
$div->eq(3)->removeAttr('style')->addClass('lo'); $div->eq(5)->removeAttr('style')->addClass('description'); } 打印 $doc; ?>

【问题讨论】:

    标签: php simplexml


    【解决方案1】:

    您需要编辑 simple_html_dom 源代码。创建自己的功能来完成原始功能会更容易。原来的(因为allow_url_fopen不能使用)是:

    function file_get_html() {
        $dom = new simple_html_dom;
        $args = func_get_args();
        @$dom->load(call_user_func_array('file_get_contents', $args), true);
        if (($error=error_get_last())!==null)
        throw new Exception($error['message']);
        return $dom;
    }
    

    你可以这样做:

    if (ini_get('allow_url_fopen')) {
          $html = file_get_html('http://www.urlhere.com/');
    }else{
          $ch = curl_init('http://www.urlhere.com/');    
          curl_setopt  ($ch, CURLOPT_HEADER, false); 
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
          $src = curl_exec($ch);    
          $html = str_get_html($src, false);
    }
    

    无需修改源代码,有效地做同样的事情。

    【讨论】:

    • 感谢 fermi 我使用了你的代码并替换了 include('simple_html_dom.php');包括('phpQuery.php');在我的代码中使用 if(ini_get... 并没有更改 simple_html_dom 源但它不起作用只显示空白屏幕
    • 好吧,添加这个:var_dump($html); 并查看 CURL 代码是否收到任何内容。
    • Fermi 它仍然显示空白页我用你的代码编辑了我上面的帖子
    • 编辑您的帖子并包含整个来源,以便我查看可能缺少的内容。
    • 而且...我看到了您的问题:将 2 个包含语句移动到文件中的第一件事:它们必须在 str_get_html 之前出现,而不是在之后,否则函数不会存在。
    【解决方案2】:

    另一种选择是使用data:// 流包装器,它允许您使用file_get_html() 函数而无需更改它。

    if (!ini_get('allow_url_fopen')) {
        $ch = curl_init($source);    
        curl_setopt($ch, CURLOPT_HEADER, false); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
        $xml_raw = curl_exec($ch); 
        curl_close($ch);   
        $source  = 'data://text/html;base64,'.base64_encode($xml_raw);
    }
    $html = file_get_html($source);
    

    此方法使用 PHP 5.2.0 及更高版本支持的 RFC 2397 数据 URL 方案。

    【讨论】:

    • 也感谢您的帖子 =) 我也会对此进行记录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2015-05-21
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多