Welcome Guest, Not a member yet? Register   Sign In
External script access to return html data

Good Day,

I have an existing report engine within a codeigniter webpage environment. The report engine is quite simple, it has a drop down of different reports which can be run and date ranges that can be filled in. When submitting, a reportPresenter.js file handles the submit action which then renders the selected report, which basically loads it's respective js loader i.e. customerReport.js which has a Datatables builder which returns a formatted report. It works perfectly when using manually, but I would now like to try and schedule some of these reports to be sent weekly via email to certain addresses. I'd like to avoid having to rebuild the entire report, as the one I'm wanting to schedule is very complex, so replicating the functionality is quite tricky, as well as it would require me to update 2 separate files whenever I need to make any sort of changes to the reports.

I've explored a solution which is feasible when working outside of a codeigniter framework, which is basically a curl approach in php to load the respective url for the report area, capture the original html, then input the selected report and dates through code, these can be hard coded, and then triggering the submit button from the php code, after which we then curl the url again, capture only the report-table <table> element and extract that out of the rest and then construct that HTML into a pdf report and attach it to the email. This works perfectly in non-restrictive environments like simple html pages, etc. However as soon as I try it in codeigniter, I get met with the regular login page which is there for security purposes. I've tried posting login credentials to the login URL in order to access the reports url, but no matter how I post the credentials through a curl request, it seems to just return an empty string as a response. I'm not too familiar with how the codeigniter environments are setup but I do understand that I likely need either some kind of API key, or session key after logging in, in order to access data past the login page, however I can't seem to find any resources that can assist me with doing this (if it is even possible).

Hoping someone who might have done something similar might be able to share their suggestions, my main aim is to basically generate a report for a certain period and then schedule this to be run through my cronjob scheduler, without having to build an entire new engine to run this.

Great explanation! Here are some options to consider:

Background Jobs: Explore CodeIgniter's built-in libraries like Queues or third-party libraries for background jobs. This allows scheduling report generation in the background without needing a separate script.

Cron Jobs with API Key:

Implement a secure API endpoint in your CodeIgniter app to access reports.
Use cron jobs to trigger the API endpoint with a pre-defined API key for authentication (avoid hardcoding credentials).
Command Line Script: Create a standalone PHP script outside CodeIgniter. This script can use cURL to access the login page, but instead of posting credentials, leverage browser automation tools (like headless Chrome) to bypass the login and directly access the report URL. Schedule this script with cron jobs.


CodeIgniter Queues: https://stackoverflow.com/questions/25382011/omeglz/codeigniter-run-code-in-background (Consider alternatives if outdated)
Third-party libraries for background jobs (search online)
Browser automation with PHP (search online)
Choose the approach that best suits your comfort level and security needs. Let me know if you have questions about any specific option!

Theme © iAndrew 2016 - Forum software by © MyBB