实时网站的自动数据库更新

I am trying to build a website that will display real-time (or close to it) data from an XML feed on another website. I know how to use jquery, AJAX, and PHP to pull the data from the MySQL database at a regular interval (every 1 to 3 seconds), what I am having trouble with is figuring out how to update the database itself with the new XML data every 1 or 2 seconds. I also need to keep a history of all the data.

I am able to write a PHP script to get the XML data and update the database just fine, but I do not know how to schedule this script to run every second without being initiated by any user. My research so far has pointed me to cron but my host does not offer it and it does not seem to be intended for such frequent updates. I don't know if PHP is the best approach either.

Any ideas or pointers would be greatly appreciated, I don't mind researching the specifics of the implementation but I do not even know how to Google my problem effectively at this point.

There are various solutions to this request.

The easiest one is to use an external service such as

http://www.webcron.org/online-cron

It can run every minute. So you could get as much data as you can and caching the data while it is inserting in the DB. But if you do not want to spend money you should go for something else.

Another way could be to create a PHP recursive function, a loop that once started never ends. So until the loop finds the data on your target website it will keep running. It means you will have to start the loop only one time. Just be sure not to echoing anything or the loop will stop once you leave the page.

Also you may need to trick a bit with your PHP.

Check also http://php.net/manual/en/function.ignore-user-abort.php

Hope it helps

Rather than scheduling such frequent updates consider storing a timestamp of the last update time and updating only if your desired interval has elapsed.

You may also consider a locking scheme to ensure that new updates are not started if another thread is already in the process of performing an update.