We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Edit Channels from Cron

How Do I?

vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

Is it possible to add new channels or edit existing ones from a cron job? Or from the command line on the server?

Assuming, here, the cron job executes a hidden template (with the proper permissions to avoid from being executed in public). The EE template has some PHP code that does the job. Not ideal but could work. Better approach, running it from the command line.

The reason I ask this is that using the channel form requires the token submission for security and for that reason it actually requires a POST submit, which would not work for example running a curl command.

There probably is a way with add-ons, but I don’t want to have to code a new add-on for every single job, which seems overkill and way too much work. I’m just curious how hard, messy or complicated this is.

If there is a simple PHP method or call to do this? I need to run some maintance jobs on a channel, and I’m curious if this can be automated and run programmatically without user interaction.

I had the impression the CLI is for this, but I suspect that is a different type of use.

       
JCOGS Design's avatar
JCOGS Design
148 posts
4 years ago
JCOGS Design's avatar JCOGS Design

I think you have asked this question in Slack - if you got an answer there apologies if this is redundant info.

It is not clear what you are trying to do with the CRON job: perhaps if you were more explicity about what you are trying to do, it might be easier for someone to answer.

It is certainly possible to add / edit channels via a triggered response (e.g. an add-on via an ACT=XX action type call, or perhaps less usefully by as you suggest hitting a specific template primed to carry out some task).

The thing that is not clear is what remote / timed / automated action do you have in mind that needs to create a channel periodically, why this has to be done using CURL, and why you think CURL cannot submit POST variables (see a good description of how here)?

There are some new CLI features coming in EE 6.1 - if you hunt around on YouTube you probably can find the videos of the sessions from the just finished EEConf 2021 - and TJDraper’s Executive-EE is also a possibility perhaps - though all this is guesswork since it is unclear what actually you are trying to do…

HTH

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

I want to be able to add specific data entries (channels) from remote systems as I have a clustered setup of automation systems.

Hence executing a cron or manually with PHP from the command line (the URL) that insert or edits data in Expression Engine. We call this programmatically, when it’s done by some automation (script, software, etc.) not a user.

But if I use EE, this means, you also have the admin CP as GUI to manipulate data if required in a visual interface. Staff can then potentially edit or update information from the control panel if required.

Yes, the command line would be perfect to achieve this. Any more information on this? I just mentioned CURL but it does not have to be CURL. I will create my own code to execute the URL or actions.

Command line which can add a new channel or update an existing channel would be great, but using PHP in a specific EE template could also work. I would just add my own authentication to it to make sure it cannot be executed except from the systems I need.

       
JCOGS Design's avatar
JCOGS Design
148 posts
4 years ago
JCOGS Design's avatar JCOGS Design

The CLI stuff is in the documentation … might be worth a read. The CLI stuff is getting updated with 6.1 - I’m guessing the documentation will catch up when that happens.

There is also the Executive-EE stuff mentioned last time - which by reputation is more powerful than possibly is healthy … (though I’ve not used it).

Updating content via an automated process is not hard to do; what the best approach is I think depends more on the kind of information you are trying to import. For some things you might be better off doing an add-on - EE has some useful functions built in such as an RSS parser and you get access to all the wonders of php directly.

Working remote updates through templates is also possible, but I guess you have to carry the overhead of EE trying to do all the things it thinks it needs to do when it renders the page etc. I would be cautious about embedding / processing php within such templates if you are concerned about security.

HTH

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

I already automate stuff running PHP code and SQL queries. The thing about EE is that it uses different tables for fields, channels, and others and then ties all the data together in a single channel entry.

If I tried to do this with standard queries I would have to run multiple queries, such as getting the field ID first, then the channel title, and so on.

If I go with my own table approach, I will keep the data structured in such a way that I can make queries as simple and efficient as possible, but I lose a quick way to edit that data from EE. I would have to manually create an interface.

If I use EE channels, I gain the quick and easy way to edit data if required from the control panel, but makes my custom code harder for automation.

Hence, I was looking for a native way that I can just execute code such as: Insert A into channel ID X.

Letting EE do all its magic when it comes to relationships with fields and other data. That way I can just use native channels for data entry but still run a simple query from a script if I need to update things.

       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

Are maybe models designed for this? From the EE documentation:

https://docs.expressionengine.com/latest/development/services/model.html

The model service helps smooth out interacting with Expression Engine’s data types by providing an API that mimics their natural language description as closely as is feasible. You do not query for channel_data joined on channel_titles; instead, you simply get a channel entry with its fields.

       
JCOGS Design's avatar
JCOGS Design
148 posts
4 years ago
JCOGS Design's avatar JCOGS Design

You certainly can work ‘in parallel’ to EE on the data tables it is built up from, and in some cases this makes sense. But when you do, as you note, the issue is making sure that your changes are complete - so that EE will properly recognise the changes.

Ensuring that your ad-hoc queries do not break EE is hard except in the simplest cases: I think you are talking yourself into working with EE’s built in data handling facilities, which offer a performant environment coupled with established classes / methods / services to use that ensure your activites are done in a way that is compatible with the EE CMS data structures.

Hooking into the EE model system is the first step - rather than writing custom queries, simply access the channel data directly via the model system and not worry about breaking stuff.

Another thing you might like to look at is EE’s implementation Active Record (similar to of CodeIgnitor’s Query Builder / ActiveRecord system) which helps you construct SQL operations - so if you do find you have to run custom queries you can build them into your php code directly: active record and the EE model systems play nicely together 😊

HTH

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.