array_slice() warning in php and WordPress

I wrote this code:

function get_feed(){

 // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss('http://dorar.shamekh.ws/?feed=rss2');
$maxitems = 1;
$items = array_slice($rss->items, 0, $maxitems,false);
return $items;

}

As a part of a plugin for WordPress , it works fine on my local server , but when I upload it to my blog I get the message:

Read More

Warning: array_slice()
[function.array-slice]: The first
argument should be an array in

php version on my local host : 5.2.6

php version on my site : 5.2.5

Related posts

Leave a Reply

4 comments

  1. It seems from the documentation that $rss->items should already be an array. I’d guess that the RSS fetch is failing. Try:

    if (is_array($rss->items)) {
       $items = array_slice($rss->items, 0, $maxitems,false);
    } else { var_dump($rss->items); }
    

    MagpieRSS combined with dorar.shamekh.ws’ (use of/configuration of) Apache 1.3.41 is leading to a very bizarre behaviour:

    A “normal” HTTP request:

    GET /feed/ HTTP/1.0
    Host: dorar.shamekh.ws   
    

    MagpieRSS’s request:

    GET /feed/ HTTP/1.0
    User-Agent: MagpieRSS/0.72 (+http://magpierss.sf.net)
    Host: dorar.shamekh.ws:80
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
    

    Note the different ‘Host’ headers. When the port number is appended, as in MagpieRSS, the site returns a 301:

    HTTP/1.1 301 Moved Permanently
    Date: Fri, 22 May 2009 02:45:03 GMT
    Server: Apache/1.3.41 (Unix) PHP/5.2.5 mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_ssl/2.8.31 OpenSSL/0.9.7a
    X-Powered-By: PHP/5.2.5
    X-Pingback: http://dorar.shamekh.ws/xmlrpc.php
    Last-Modified: Wed, 20 May 2009 22:03:05 GMT
    ETag: "e591693fdf2d27ee7dae19e256db2f46"
    Location: http://dorar.shamekh.ws/feed/
    Connection: close
    Content-Type: text/html
    
  2. What about casting $rss->items as an array first:

    function get_feed(){
    
     // Get RSS Feed(s)
    include_once(ABSPATH . WPINC . '/rss.php');
    $rss = fetch_rss('http://dorar.shamekh.ws/?feed=rss2');
    $maxitems = 1;
    $rss->items = (array) $rss->items;
    $items = array_slice($rss->items, 0, $maxitems,false);
    return $items;
    
    }
    
  3. It sounds like to me that when it is attempting to get the RSS it is failing. Perhaps fetch_rss uses file_get_contents which has been disabled for URLs.

    Either that, or for some reason, the property $rss->items isn’t an array for some reason.