0

我想在使用 smarty 的网页上显示一些最新帖子。我找到了一些脚本并在某处进行了更改,但我没有让它工作。

在模板文件中,我插入了下一个代码:

{sr_wpfeed var="someposts" wpsite="http://www.digins.nl/blog" num=3}
                            {foreach from=$someposts item=somepost}
                                <h2><a href="{$somepost.link}">
                                {$somepost.title}
                                </a>
                                </h2>
                                {$somepost.description}
                                <br>
                            {/foreach}

所以我调用了我的 php 文件中的函数 sr_wpfeed:

    function smarty_function_sr_wpfeed($params, &$smarty) {

  $required_params = array('var','wpsite', 'num');

  foreach($required_params as $_key => $reqd_param) {
    if (!isset($params[$reqd_param]))
      $smarty->trigger_error("sr_wpfeed: required attribute '$reqd_param' not passed", E_USER_ERROR);
  }

  $var      = '';
  $wpsite = '';
  $num = 0;
  foreach($params as $_key => $_val) {
    switch($_key) {
      case 'wpsite':
      case 'var':
          if (!is_array($_val)) {
              
                $$_key = $_val;
              
          } else
              $smarty->trigger_error("sr_wpfeed: '$_key' cannot be an array", E_USER_ERROR);
          break;
          
      case 'num':
          if (!is_array($_val)) {
            $$_key = (int)$_val;
            if ($$_key < 0 || $$_key >25)
                $smarty->trigger_error("sr_wpfeed: '$_key' not between 1 and 25", E_USER_ERROR);
          } else
              $smarty->trigger_error("sr_wpfeed: '$_key' cannot be an array", E_USER_ERROR);
          break;

      default:
          $smarty->trigger_error("sr_wpfeed: attribute '$_key' not recognized", E_USER_NOTICE);
          break;
    }
  }
   
  //if(!$xml=simplexml_load_file($wpsite.'/feed') ){
  // $smarty->trigger_error('Error reading XML file',E_USER_ERROR);}

  $xml=simplexml_load_file($wpsite.'/feed');
  
  $posts = array();
    foreach($xml as $item){  
        for($i=0; $i<count($item->item); $i++){
            if($i<$num){
        $posts[] = array('link'         =>  (string)$item->item[$i]->link
                       , 'title'        =>  htmlentities($item->item[$i]->title,ENT_COMPAT, 'UTF-8')
                       , 'description'  =>  $item->item[$i]->description
                       );
      }
        }
    }
  

  $smarty->assign($var, $posts);
}

我认为 xml 文件有问题。在 simplexml_load_file 函数之前,它似乎工作正常。有任何想法吗?

第二个问题是 trigger_error;它挂起我的网站。

更新:下面的错误日志:

Warning: simplexml_load_file() [function.simplexml-load-file]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/vhosts/digins.nl/httpdocs/assets/includes/footer.inc.php on line 44


Warning: simplexml_load_file(http://www.digins.nl/blog/feed) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found in /home/vhosts/digins.nl/httpdocs/assets/includes/footer.inc.php on line 44


Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://www.digins.nl/blog/feed" in /home/vhosts/digins.nl/httpdocs/assets/includes/footer.inc.php on line 44


Warning: Invalid argument supplied for foreach() in /home/vhosts/digins.nl/httpdocs/assets/includes/footer.inc.php on line 47

更新2

我将 simplexml_load_file 更改为使用 curl:

$wpsite = $wpsite.'/feed';
   $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $wpsite);
  curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  $returned = curl_exec($ch);
  curl_close($ch);
  // $xml === False on failure
  $xml = simplexml_load_string($returned);

起初它似乎不起作用,因为我的测试 WordPress 只有一个帖子。在这种情况下, $xml 不是在 foreach 上给出错误的数组

4

0 回答 0