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 part of a WordPress plugin, it works fine on my local server, but when I upload it to my blog, I get this message:

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

php version on my localhost: 5.2.6

php version on my site: 5.2.5

0


a source to share


4 answers


As you can see from the documentation, it $rss->items

should already be an array. I am assuming the RSS fetch is not working. 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' (usage / configuration) Apache 1.3.41 results in very strange behavior:

"Regular" HTTP request:

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

      



MagpieRSS 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 a port number is added, as in MagpieRSS, the site returns 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

      

+1


a source


How to distinguish first $ rss-> elements as an array:



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;

}

      

0


a source


It seems to me that when it tries to get the RSS, it fails. Possibly fetch_rss

using file_get_contents which has been disabled for urls.

Either that, or for some reason the $ rss-> property is not an array for some reason.

0


a source


fetch_rss()

it is deprecated. See: http://codex.wordpress.org/Function_Reference/fetch_rss

You must use fetch_feed()

0


a source







All Articles