Welcome Guest, Not a member yet? Register   Sign In
Submitting a form with a cron job?
#1

(This post was last modified: 03-26-2020, 01:54 PM by SmokeyCharizard.)

Hey fellas, its me again.

I'm trying to write a cron job for my codeigniter app, but I have now idea where to start. 

So far, I created a controller called 'cron' with the following:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//0 0 0 0 0 /usr/bin/php /home/your_site_user/public_html/index.php cron updateAge
class Cron extends CI_Controller 
{
    /**
     * This is default constructor of the class
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->library('input');
        $this->load->model('cron_model');
    }
    
    
/**
     * This function is used to update the age of users automatically
     * This function is called by cron job once in a day at midnight 00:00
     */
    public function addProducts()
    {
        // is_cli_request() is provided by default input library of codeigniter
        if($this->input->is_cli_request())
        {            
            
// $this->cron_model->addProducts();
        }
        else
        {
            echo "You dont have access";
        }
    }
}
?>
 
My cron job has to submit form data in a view, and I thought this would help, but I don't think it will work.

I can't find any resources online for making cron jobs in codeigniter, so am I on the right track? I know that I need to execute the cron job file itself with a command (I found help here for that), but this is as far as I have gotten. 

Please help!
Reply
#2

To run your code from a cron job, you have to call your controller via the CLI: https://chez14.gitlab.io/codeigniter-doc...l/cli.html

And to submit the form, you can use CURL. You can find a lot of example with Google.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(03-26-2020, 02:15 PM)includebeer Wrote: To run your code from a cron job, you have to call your controller via the CLI: https://chez14.gitlab.io/codeigniter-doc...l/cli.html

And to submit the form, you can use CURL. You can find a lot of example with Google.

Thanks a BUNCH! So the CURL should just be sent to the same controller the form is being sent by the view?
Reply
#4

If you want to submit a form to another website, use CURL. You will be able to make a POST request and send the fields with their associated values.

But if you want to send a form to your own application, I’m not sure it’s the best way. I don’t see a reason why you would need to use a form when you can just call the model or library you need from the controller.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

(03-26-2020, 02:25 PM)includebeer Wrote: If you want to submit a form to another website, use CURL. You will be able to make a POST request and send the fields with their associated values.

But if you want to send a form to your own application, I’m not sure it’s the best way. I don’t see a reason why you would need to use a form when you can just call the model or library you need from the controller.

Thanks for you help! I just tried to run the script, and I got this error:
PHP Code:
An uncaught Exception was encountered

Type
:        RuntimeException
Message
:     Unable to locate the model you have specifiedCron_model
Filename
:    /opt/lampp/htdocs/codeigniter/system/core/Loader.php
Line Number
348 
How do I add Cron_model to my project?
Reply
#6

You load it like any other model?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB