Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-05-24 01:46:57

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 585
Website

Posting articles via back-end PHP script?

Hi all, hope you are doing OK in these pandemonious times!

I have some third-party data sources I’d like to collect and add to my site via a back-end cron job. First, would this work? Second, I have some questions:

  • Collect latest RSS from provided sources <— OK so far
  • Add formatting <— No idea what will be needed for best interoperation with TXP
    • Do I need to format this into textile? How will formatting work for the article body?
  • Insert into DB <— recommendations for best practices appreciated!
    • Set publish date to 2 days from now
  • Send me an email with the edit link to review

Thanks for any input, all assistance is appreciated.

Edit: Just to clarify, I know I can add XML feeds into my site via plugins, but this is more like intentionally posting articles that are collections of data, with additional editing and customization added in.

Last edited by maruchan (2020-05-24 02:00:06)

Offline

#2 2020-05-24 20:22:39

gaekwad
Multi-hyphenate
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 3,559
GitHub

Re: Posting articles via back-end PHP script?

This sounds fun! High level – untested, and this is what I would do, so season to taste:

  • use xsltproc or similar to chew through each XML content block, spit out HTML, tweak formatting until you’re happy with how it looks
  • use pandoc or similar to convert from HTML into escaped SQL-friendly title + article files
  • use some shell magic to wrap the SQL-friendly files above into a single .sql file with some NOW() + INTERVAL 2 DAY magic in the timestamp field, dump into a holding directory
  • set a cron task to process any .sql files in said holding directory at a given schedule

I’m running with scissors with some of the above, having not done it before, but it might be of some use.

Offline

#3 2020-05-26 17:04:07

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 585
Website

Re: Posting articles via back-end PHP script?

Thank you gaekwad, I appreciate those ideas. Seems like I have a lot of testing ahead of me. I’ll try to report back on how it goes.

Offline

#4 2021-06-03 09:28:56

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 289
Website GitHub Twitter

Re: Posting articles via back-end PHP script?

Hello, any progress on your purpose?

Actually I plan almost the same. The php script with fetching the RSS and reformating the article in textile style is ready, but I don’t know how to post the entire article. I have title, excerpt and article body, but how to post that automatically?

I would prefer something with using internal textpattern functions, for example in a hidden section and triggering the script via a url parameter in a cron job. But for that intention I need to call which function?

As far as I understand the plans to replace xmlrpc with an api are not mature, but that would be an ideal solution.

Offline

#5 2021-06-03 09:51:04

etc
Developer
Registered: 2010-11-11
Posts: 4,331
Website GitHub

Re: Posting articles via back-end PHP script?

I think this plugin could be adapted for publishing articles. It passes via the standard txp interface, so all validation is done. Need to recall how it works, though.

Offline

#6 2021-06-03 09:53:24

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 289
Website GitHub Twitter

Re: Posting articles via back-end PHP script?

Cool, thank you for your quick reply. I’ll try it within the next days.

Offline

#7 2021-06-05 17:16:41

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 289
Website GitHub Twitter

Re: Posting articles via back-end PHP script?

I found a working solution for my intention in txplib_wrapper.php, but I fear a little bit, that you’ll remove this wrapper function in a future version??

include_once txpath.'/lib/txplib_wrapper.php';

$wrapper = new TXP_wrapper('newsbot', 'the_newsbot_password');
if ($id = $wrapper->newArticle(array(
     'Title' => $title,
     'Body'  => $body,
      'Excerpt' => $excerpt,
      'Status' => '4' ,
      'Section' => 'article',
      'Category1' => 'autonews', 
      'textile_body' => '1',
      'textile_excerpt' => '1', 
      'url_title' => '',
      'Image' => $image,

)) !== false)

Would like to use etc_post directly in php to use it for uploading an article image, but I don’t know how to pass this import stuff?

<txp:etc_post event="image" step="image_list">
import="https://dev-demo.textpattern.co/dev/images/10.jpg"
</txp:etc_post>

something like that, doesn’t work:

etc_post(array(
        'event' => 'image',
        'step' => 'image_list',
        $thing => array('import' => $image),
     ));

Offline

#8 2021-06-05 21:22:28

etc
Developer
Registered: 2010-11-11
Posts: 4,331
Website GitHub

Re: Posting articles via back-end PHP script?

whocarez wrote #330370:

Would like to use etc_post directly in php to use it for uploading an article image, but I don’t know how to pass this import stuff?

General tag signature is tag(array $atts, string $thing), so you should pass it

etc_post(array(
        'event' => 'image',
        'step' => 'image_list'
    ),
    'import='.$image
);

This plugin expects its $thing to be ini or json string, but you can certainly modify it as needed.

Offline

#9 2021-06-06 08:22:41

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 289
Website GitHub Twitter

Re: Posting articles via back-end PHP script?

Thanks! That’s working perfectly. So in principle the major problems are solved, for posting something in Textpattern via a script/plugin.
Remains only the minor problem, for excluding these autoposted articles from the main article list on the frontpage. Maybe it is better to try, to realise that via a custom_field entry instead of a category.

Offline

#10 2021-06-06 09:24:00

etc
Developer
Registered: 2010-11-11
Posts: 4,331
Website GitHub

Re: Posting articles via back-end PHP script?

whocarez wrote #330379:

Remains only the minor problem, for excluding these autoposted articles from the main article list on the frontpage. Maybe it is better to try, to realise that via a custom_field entry instead of a category.

Yes, <txp:article /> ignores category attribute, but honours custom fields. You could also publish these articles in a section that is not on frontpage.

Offline

#11 2021-06-06 09:50:55

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 289
Website GitHub Twitter

Re: Posting articles via back-end PHP script?

Ah, yes, that should be the best solution. Publish these articles in an extra section like “autopost” and so they can easily be ignored or “filtered” in the main txp:article tag on the frontpage.

Offline

#12 2021-06-07 12:56:41

etc
Developer
Registered: 2010-11-11
Posts: 4,331
Website GitHub

Re: Posting articles via back-end PHP script?

I have added an example to etc_post v.0.2 to illustrate articles submission.

Offline

Board footer

Powered by FluxBB