在simplepie.php中为foreach()提供的参数无效

时间:2014-04-08 13:03:25

标签: php joomla foreach joomla3.0 simplepie

如何在以下foreach中修复error Message: Invalid argument supplied for foreach() - Line Number: 71

错误是:

Warning: Invalid argument supplied for foreach() in /home/u712773673/public_html/components/com_cedphotofeed/parser/simplepie.php on line 71

第71行是:

71.                    foreach ($thumbnails as $thumbnail) {
72.                        $modelItem['thumbnails'][] = $thumbnail;
73.                    }

<?php
/**
* @version      2.6.3
* @package      cedPhotoFeed
* @copyright    Copyright (C) 2009-2013 Cedric Walter. All rights reserved.
* @copyright    www.cedricwalter.com / www.waltercedric.com
*
* @license        GNU/GPL v3.0, see LICENSE.php
*
* cedPhotoFeed is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// no direct access
defined('_JEXEC') or die('Restricted access');

require_once(JPATH_SITE . '/libraries/simplepie/simplepie.php');

class PhotoFeedSimplePieParser
{

    var $simplepie = null;

    function PhotoFeedSimplePieParser($config = array())
    {
    }

    public function getItems($params, $feedUrl, $startAtPhoto, $limit)
    {
        //Init SimplePie
        $this->simplepie = new SimplePie($feedUrl, null, $params->get('RssCachetime', 3600));

        // set sorting before parsing feed
        // Reorder feed by date descending
        $this->simplepie->enable_order_by_date(false);

        //do the parsing
        $this->simplepie->handle_content_type();
        //$feedEntries = $this->simplepie->get_item_quantity();

        $imageCount = ($startAtPhoto + $limit);
        if ($startAtPhoto == "") {
            $startAtPhoto = 0;
        }
        //get_items($startAtPhoto, $imageCount) cant be used
        $items = $this->simplepie->get_items($startAtPhoto, $imageCount);

        $model = array();
        foreach ($items as $item) {
            if ($enclosure = $item->get_enclosure()) {
                $modelItem = array();
                $modelItem['title'] = htmlspecialchars(strip_tags($item->get_title()));
                $modelItem['description'] = htmlspecialchars(strip_tags($item->get_description()));
                $modelItem['mediaContentUrl'] = $enclosure->get_link();

                $media_group = $item->get_item_tags('http://search.yahoo.com/mrss/', 'group');
                $media_content = $media_group[0]['child']['http://search.yahoo.com/mrss/']['content'];
                $attributes = $media_content[0]['attribs'][''];

                $modelItem['width'] = $attributes['width'];
                $modelItem['height'] = $attributes['height'];
                $modelItem['imageUrl'] = $attributes['url'];

                $modelItem['thumbnails'] = array();
                $modelItem['links'] = array();
                foreach ($item->get_enclosures() as $enclosure) {
                    $thumbnails = $enclosure->get_thumbnails();
                    $modelItem['links'][] = $enclosure->get_link();
                    foreach ($thumbnails as $thumbnail) {
                        $modelItem['thumbnails'][] = $thumbnail;
                    }
var_dump($thumbnails);
                }

                $model[] = $modelItem;
            }

        }


        return $model;
    }

    public function getGalleryLink()
    {
        return $this->simplepie->get_link();
    }

    public function getGalleryTitle()
    {
        return $this->simplepie->get_title();
    }

    public function getGalleryDescription()
    {
        return $this->simplepie->get_description();
    }

}

2 个答案:

答案 0 :(得分:0)

也许你的阵列可能是空的。也不能是阵列。检查两个......

将第一行更改为存在:

//Check if the thumbnails aren't empty
if(count($thumbnails)>0)
{
    //Check if is array
    if(is_array($thumbnails))
    {
        foreach ($thumbnails as $thumbnail) 
        {
            $modelItem['thumbnails'][] = $thumbnail;
        }
    }
    //You can print a message here if aren't array
}

答案 1 :(得分:0)

将代码更改为:

if(is_array($thumbnails) && count($thumbnails)){
    foreach ($thumbnails as $thumbnail) {
         $modelItem['thumbnails'][] = $thumbnail;
     }
}