duanpi7107 2011-05-05 14:34
浏览 82
已采纳

allow_url_fopen的simplexml补丁用于simplehtmldom

My hosting company has disabled allow_url_fopen & allow_url_include. They recommend that I use cURL instead, then I saw this patch on 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);  }

and it works on my hosting so I was wondering if I could use it on simplehtmldom here is the code I am using

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

Can i use this? If yes what changes should I make, thank you for your help

Edited above code

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

Outlook

find('.SynopsisContainer span') as $e) echo $e->innertext . '
'; ?>

4 Day Weather Forecast

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'); } print $doc; ?>

  • 写回答

2条回答 默认 最新

  • duanhuang4306 2011-05-05 14:41
    关注

    You'd need to edit the simple_html_dom source. Its easier to just create your own function that does what the original does. The original (which you can't use because of allow_url_fopen) is:

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

    You can just do this:

    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);
    }
    

    No need to modify the source, and effectively does the same thing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加