0

I want to show some latest post on a webpage which uses smarty. I found some script and changed it somewhere, but I did not get it working.

On the template files I inserted the next code:

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

So I call the function sr_wpfeed which is in my php file:

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

I think something goes wrong with the xml file. It seems to work ok until the simplexml_load_file function. Any ideas?

Second problem is the trigger_error; it hangs my website.

Update: The error log below:

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

Update2

I changed the simplexml_load_file to use 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);

At first it seems not to work because my test WordPress had only one post. In that case the $xml is not an array which gave an error on the foreach

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
PeterZ
  • 1
  • 2
  • "I think something goes wrong with the xml file." Could you explain what actually happens? At the moment, we just have a large block of code, and the fact that it didn't do what you hoped. Do you get [an error](http://stackoverflow.com/q/12769982/157957)? Have you [turned on error reporting](http://stackoverflow.com/a/12772851/157957)? – IMSoP Oct 30 '13 at 20:10
  • I am quit sure there is something wrong withe reading the xml file. i tried several XML feeds from different websites. Actualy i think there must be something wrong with the simplexml_load_file function. I cannot find out what. The array $posts remain empty. I did not check the error reporting yet. I am going to have a look at it now. thanks peter – PeterZ Oct 30 '13 at 20:37
  • I don't disbelieve that there is something wrong, but you need to tell us what it is, otherwise we can't help. Without knowing what you've tried, what output you've seen, etc, there are dozens of different problems we could guess you might be having... – IMSoP Oct 30 '13 at 20:41
  • A part of the error log below: 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 – PeterZ Oct 30 '13 at 20:50
  • You need to click "edit" on the question and add the missing details, not try to squeeze it into a comment. – IMSoP Oct 30 '13 at 20:51
  • OK, now you have the error message; if you read it, you will see that it gives you quite a specific clue as to what's wrong: "http:// wrapper is disabled in the server configuration by allow_url_fopen=0" So there is a setting called allow_url_fopen, it's set to 0, and this means the code can't run. You can probably work the rest out from there, with a few basic web searches. – IMSoP Oct 30 '13 at 21:06
  • 1
    checked phpinfo and indead the 'allow_utl_fopen' is switched off. I cheked the documentation of my host and they do not switch it on. – PeterZ Oct 30 '13 at 21:43

0 Answers0