Create a Laravel / PHP script that runs periodically e.g. every 1 week to update my MySQL database with calls to an api

620

The reason for my question is because I've tried to implement this by just calling the api from my code within my website, but because there so many calls the initial page times out after 30 seconds, and you don't really want your users waiting longer than 2 / 3 seconds for a page on a website to load.

So I was wondering how I would write a Laravel / PHP function that would pull all the information from the API calls into my MSQL database and query the database instead. I was also wondering how I would get this to run periodically as mentioned in the subject.

Please let me know your thoughts, and what you reckon would be the best approach going forward in accomplishing this task which I was hoping would have been a lot more trivial than what it's turned out to be.

597

Answer

Solution:

Scheduling will not call your API directly. the way is, when you create a scheduling command, then there you should Write your code which will trigger the API(using curl, file_get_contents or any suitable method), it will give you response and you can manipulate it according to your requirement.

Once done, you have to simply execute the command in terminal or through cron settings, that command will trigger your code(API call which you wrote in command handle method)

Follow below documentation, let me know if you have any difficulties in implementation

https://laravel.com/docs/5.1/scheduling

This is video tutorial, creation of scheduling command and accessing

https://www.youtube.com/watch?v=mp-XZm7INl8

People are also looking for solutions to the problem: php - Count surplus entries of duplicate values in an array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.