Ping API服务器以获取PHP中的更改

问题描述:

I am importing the products from a third party website, I'm using their web services to import the products. Now what is the best way to check for the API changes and products updates automatically and if changed or new products exists then load them. Can i use ajax to ping the server after a specific amount of time or any other alternative way to achieve this ?

我正在从第三方网站导入产品,我正在使用他们的网络服务导入产品。 现在,自动检查API更改和产品更新的最佳方法是什么,如果存在更改或新产品,则加载它们。 我可以使用ajax在特定时间或其他任何其他方法实现此操作后ping服务器 ? p> div>

Javascript handle timer nicely and reliabily. Only major drawback is that you need to keep the page opens all the time in your browser. Here is a jquery example of a repeated task calling an ajax (jquery is only used to provide a quick ajax, setInterval() function is native javascript)

var timerhandler;

$(document).ready(function() {

    function CheckNow()
    {

        $.ajax(
        {
            type: 'GET',
            url: '<your local php script calling the remote API>',
            success: function(data) 
            {
                //print out here the result of your script work
            }
        });

    }

    timerhandler = setInterval(CheckNow,50000);
    //50000 = 50 seconds
});

This is good for test but not good for production server. For production you should use scheduled task provided by the host OS. On linux-based : a cron task running wget with your php script as target.