phpsimplepie

Array to String Conversion Error while tweaking Simplepie


I am using SimplePie library for feed generation. I made some changes for desired output. It was working great till today. Here is my code:

<?php
header('Content-type: text/plain; charset=utf-8'); 
// Include the SimplePie library
// For 1.0-1.2:
#require_once('simplepie.inc');
// For 1.3+:
require_once('autoloader.php');

// Create a new SimplePie object
$feed = new SimplePie();

// Instead of only passing in one feed url, we'll pass in an array of three
$feed->set_feed_url(array(

'http://localhost/full-text-rss/makefulltextfeed.php?url=dynamic.feedsportal.com%2Fpf%2F555218%2Fhttp%3A%2F%2Ftoi.timesofindia.indiatimes.com%2Frssfeedstopstories.cms&key=1&hash=759dbac5d2121d4b7bc31ee9119d65a44a73fb6d&max=10&links=preserve&exc=1&format=json'

));
//Caching is enabled
$feed->enable_cache(true);

//timeout
$feed->set_timeout(40);

// We'll use favicon caching here (Optional)
//$feed->set_favicon_handler('handler_image.php');

// Initialize the feed object
$feed->init();

// This will work if all of the feeds accept the same settings.
$feed->handle_content_type();



 if ($feed->error)
 { 
 echo $feed->error; 
 }

 $newspaperName = array('Google News');  

 $i = 0;
 $j = 0;
 $response = array(); 
 $response["NewsItems"] = array(); 
 foreach ($feed->get_items() as $item)
 {


        $feed = $item->get_feed();

        //$permalink =
        //$NewsItems["Count"] = html_entity_decode($feed,ENT_QUOTES, "UTF-8");//html_entity_decode($item->get_id(),ENT_QUOTES, "UTF-8");



        $NewsItems["ArticleLink"] = html_entity_decode($item->get_permalink(),ENT_QUOTES, "UTF-8");//$permalink;

        $NewsItems["Title"] = html_entity_decode($item->get_title(), ENT_XHTML, "UTF-8"); 

        $NewsItems["Content"]= html_entity_decode($item->get_content(), ENT_XHTML, "UTF-8"); 
        if($i==10)
        {

         $i = 0;
         $NewsItems["Source"] = html_entity_decode($newspaperName[$j+1],ENT_QUOTES, "UTF-8");
         $j++;
        }
        else
        {

         $NewsItems["Source"] = html_entity_decode($newspaperName[$j],ENT_QUOTES, "UTF-8");;
         $i++;
        }

        $feed = $item->get_feed(); 

        $NewsItems["SourceLink"]= html_entity_decode($feed->get_permalink(), ENT_XHTML, "UTF-8");

        $feed = $item->get_feed(); 

        $NewsItems["SourceTitle"] =       html_entity_decode($feed->get_title(), ENT_XHTML, "UTF-8");

        $NewsItems["Date"]= html_entity_decode($item->get_date('j M Y |   g:i a T'), ENT_XHTML, "UTF-8");

        array_push($response["NewsItems"], $NewsItems); 
}
echo json_encode($response);
?>

But Today suddenly I got an error:

Notice: Array to string conversion in C:\xampp\htdocs\simplepie\IndiaEnglishTopStories.php on line 38
Array{"NewsItems":[]} 

Line 38 is:

echo $feed->error;

I am not a php developer so my knowledge is very much limited. I read so many answer here and I tried to implement that but still problem is there. Forgive me if you find this question stupid and annoying. Thank you for your time.


Solution

  • Your trying to echo a array this is not possible. $feed->error is an array.

    Change to

    foreach($feed->error as $err)
      echo $err;
    

    or

    var_dump($feed->error)
    

    or

    print_r($feed->error)
    

    How to echo or print an array in PHP?