UPDATE: Question edited to be clearer.
I had an issue where it seems calling a plugin’s controller via an URL caused the site itself to throw a 500 error (i.e. not being accessible at all).
The plugin loads external cinema events (as an XML) and creates entries for films and shows. I’ve made it accessible in the CP, where I hit a button to update, and it will return a log which gets printed out in the CP. Triggering it from the plugins page in the CP works perfectly. Now I wanted to create a cronjob that loads the external data on regular intervals – basically calling the plugin. After some trail and error, I managed to make the plugin’s controller callable from the outside.
So my question is – is there anything special to be aware of when calling plugins from the outside? Is there a difference if it’s called as a cronjob or if it’s done manually through the browser.
Here is the code of the controller:
<?php
namespace Craft;
class UpdateProgram_ProgramController extends BaseController
{
// Allowing to be called without being logged in
protected $allowAnonymous = true;
public function actionUpdate()
{
// Disabling having to call the plugin by POST, to access directly by URL
// this->requirePostRequest();
$log = craft()->updateProgram_program->update();
// If there are entries in the log, changes have happened
if ( count($log) ) {
craft()->userSession->setNotice(Craft::t('Program updated.'));
craft()->urlManager->setRouteVariables(array(
'log' => $log
));
}
// Otherwise, give an error message
else {
craft()->userSession->setNotice(Craft::t('Nothing to update!'));
}
}
}
And this is how I'm calling it from the cronjob:
MAILTO=[my.email@address]
# Run updateProgram every night at 3am
0 3 * * * wget http://[my.site]/actions/updateProgram/program/update > /dev/null
When trying it from the browser, I used the same URL
craft()->updateProgram_program->update()
could also be related here as well. – Matt Stein♦ Jun 5 '17 at 19:36$entry->authorId = craft()->userSession->getUser()->id;
Basically, that I'm trying to set a user, which I guess doesn't exist from the cronjob. This may explain why I can now run it in the browser (and it works), as I'm logged in to the CP. Can I save entries without being logged in? – KSP Jun 5 '17 at 23:23craft()->updateProgram_program->update()
calling Craft'sEntryService->saveEntry()
? Or is it using$entry->authorId = craft()->userSession->getUser()->id;
directly? Might help to post the contents of thatupdate()
method. – Brad Bell Jun 5 '17 at 23:30