back to phpList.org

Can't use custom attribute with RssFeedPlugin

Tags: #<Tag:0x00007f6c7e928848>

I tried adding a summary field to my RssFeedPlugin custom feed elements. However, now when I try to fetch the feed, I get the following errors:

Fetching https://zak.io/feed.xml

mysqli_real_escape_string() expects parameter 2 to be string, array given

#0 [internal function]: phpList\plugin\Common\Exception::errorHandler(2, 'mysqli_real_esc...', '...', 419, Array)
#1 .../public/admin/mysqli.inc(419): mysqli_real_escape_string(Object(mysqli), Array)
#2 .../public/admin/plugins/RssFeedPlugin/DAO.php(86): sql_escape(Array)
#3 .../public/admin/plugins/RssFeedPlugin/Controller/Get.php(163): phpList\plugin\RssFeedPlugin\DAO->addItemData(62, Array)
#4 .../public/admin/plugins/RssFeedPlugin/Controller/Get.php(186): phpList\plugin\RssFeedPlugin\Controller\Get->getRssFeeds(Object(phpList\plugin\RssFeedPlugin\DAO), Array)
#5 .../public/admin/plugins/Common/Controller.php(114): phpList\plugin\RssFeedPlugin\Controller\Get->actionDefault()
#6 .../public/admin/plugins/Common/Main.php(40): phpList\plugin\Common\Controller->run('default')
#7 .../public/admin/plugins/RssFeedPlugin/get.php(13): phpList\plugin\Common\Main::run(Object(phpList\plugin\RssFeedPlugin\ControllerFactory))
#8 .../public/admin/index.php(769): include('...')
#9 {main}

It seems like it doesn’t like that PicoFeed\Parser\Item->getTag(...) returns an array in RssFeedPlugin\Controller\Get->getCustomElementsValues(). I tried changing getCustomElementsValues() to concatenate the array to a string before it returns, but that causes more issues. I’m wondering if I somehow set up the custom field incorrectly instead?

@zakkolar If you are trying to include the media:thumbnail element then the url needs to be in the body of the element, not in the url attribute

<media:thumbnail url="https://zak.io/assets/images/posts/2020-02-22-share-unpublished-scratch-projects-with-scratch-project-viewer/teaser1.png"/>

it needs to be
<media:thumbnail>https://zak.io/assets/images/posts/2020-02-22-share-unpublished-scratch-projects-with-scratch-project-viewer/teaser1.png</media:thumbnail>

@zakkolar There’s another problem I noticed. The summary element within each entry is not being used, the parser is taking the content element instead.

I think that it is to do with namespaces. Are you able to ensure that the root feed element has the namespace shown here https://validator.w3.org/feed/docs/atom.html to see whether that makes a difference.

Update - sorry I can now see that the feed element does have a namespace.

@zakkolar After digging a bit deeper, it appears that RSS and Atom feeds are not treated in exactly the same way which is why the summary element of the Atom feed is not being used. Atom feeds are sensitive to the namespace, whereas RSS feeds are not.
That also seems to make handling custom attributes more difficult. If it is possible to generate an RSS feed then I would suggest that.

1 Like

Thank you for investigating this so thoroughly! I’ll look into switching to RSS.

@zakkolar I have made a couple of minor changes to support getting an attribute value of a custom element, and also to use the summary element instead of the content element to be included in the generated email.
You can use a placeholder similar to this to get an attribute value [media:thumbnail@url]

If you intend to use that to display an image then add some html to the RSS template. You will want to restrict the displayed size of the image because they are large

<img src="[media:thumbnail@url]" width="100px"/>

You can update the plugin on the Manage Plugins page.

Thank you so much!

I tried again and now it doesn’t give an error when I fetch new posts with summary listed as a custom attribute. However, it doesn’t seem to recognize that as a placeholder in campaigns when I send it out. It leaves [summary] in the email when it is sent.

But for now, I just set up a second feed specifically for emails that places the summary in the main content area and that’s working perfectly.

You still need to use the [content] placeholder even though the “content” is from the summary element. The PicoFeed library presents an interface that tries to be independent of the actual feed, RSS or Atom.

1 Like

Thank you!

Now I’m running into one more issue. I’m trying to use two custom attributes (the thumbnail and a new one I added for alt text). I have them stored in the settings on separate lines. No matter the order I put those attributes, only the top one seems to save. The other one stays a placeholder in the email. I’ve tried reversing them and whichever is listed first is the one that works.

Out of curiosity, I tried adding print_r($values) right before the return in RssFeedPlugin\Controller\Get->getCustomElementsValues(). When I check for new feed items, the value of $values appears correct - both the thumbnail URL and alt text are present in the array. But somewhere along the line, that doesn’t seem to get passed to the message.

Sorry, the reverse is true. Whichever custom attribute I list second is the one that works correctly in the email.

Figured it out!

When pulling the list of custom elements from the settings, it looks like there’s a stray carriage return (\r) at the end of each line. (I don’t know if this is browser-specific, but I’m using Chrome on a Mac). It seems like the carriage return is confusing the database when it attempts to save the custom attributes.

trim()ing the custom array elements before fetching their values seems to work. I changed line 122 in RssFeedPlugin\Controller\Get to:

array_map(function($i){return trim($i);}, explode("\n", $customElementsConfig));

and the issue seems to be resolved.

@zakkolar Yes your are right. Your browser appears to be sending CR NL not simply NL. I have handled line endings correctly in some other places but not here, so I will change it.