Cronjob 但适用于 jQuery/Javascript

发布于 2024-10-11 19:00:41 字数 2165 浏览 2 评论 0原文

我正在尝试开发一个主要使用 PHP 的 Web 应用程序,但我正在使用 jQuery/Javascript 从人们的 URL 中获取推文: http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=

问题是想要运行 PHP cron 作业来获取已注册我的应用程序的人的最新推文。但我不知道如何用 javascript 做到这一点?

这可能吗?

编辑:

这是 javascript 代码,我可以在 PHP 中执行此操作,以便我可以使用 Cron 作业吗?

    $(document).ready( function() {

        var url = "http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?";
        $.getJSON(url,
        function(data){
            $.each(data, function(i, item) {
                $("#twitter-posts").append("<p>" + item.text.linkify() + " <span class='created_at'>" + relative_time(item.created_at) + " via " + item.source + "</span></p>");
            });
        });
    });

    String.prototype.linkify = function() {
        return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
    return m.link(m);
  });
 }; 
  function relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      delta = delta + (relative_to.getTimezoneOffset() * 60);

      var r = '';
      if (delta < 60) {
        r = 'a minute ago';
      } else if(delta < 120) {
        r = 'couple of minutes ago';
      } else if(delta < (45*60)) {
        r = (parseInt(delta / 60)).toString() + ' minutes ago';
      } else if(delta < (90*60)) {
        r = 'an hour ago';
      } else if(delta < (24*60*60)) {
        r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
      } else if(delta < (48*60*60)) {
        r = '1 day ago';
      } else {
        r = (parseInt(delta / 86400)).toString() + ' days ago';
      }

      return r;
}
function twitter_callback ()
{
    return true;
}

I'm trying to develop a web application that mainly uses PHP but i'm using jQuery/Javascript to grab people's Tweets from their URL: http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?

The thing is want to run a PHP cron job to grab latest tweets from people who have signed up for my application. But i dont know how to do this with javascript?

Is this possible?

EDIT:

This is the javascript code, can i do this in PHP so i can use a Cron Job?

    $(document).ready( function() {

        var url = "http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?";
        $.getJSON(url,
        function(data){
            $.each(data, function(i, item) {
                $("#twitter-posts").append("<p>" + item.text.linkify() + " <span class='created_at'>" + relative_time(item.created_at) + " via " + item.source + "</span></p>");
            });
        });
    });

    String.prototype.linkify = function() {
        return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
    return m.link(m);
  });
 }; 
  function relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      delta = delta + (relative_to.getTimezoneOffset() * 60);

      var r = '';
      if (delta < 60) {
        r = 'a minute ago';
      } else if(delta < 120) {
        r = 'couple of minutes ago';
      } else if(delta < (45*60)) {
        r = (parseInt(delta / 60)).toString() + ' minutes ago';
      } else if(delta < (90*60)) {
        r = 'an hour ago';
      } else if(delta < (24*60*60)) {
        r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
      } else if(delta < (48*60*60)) {
        r = '1 day ago';
      } else {
        r = (parseInt(delta / 86400)).toString() + ' days ago';
      }

      return r;
}
function twitter_callback ()
{
    return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-10-18 19:00:41

javascript 方法 setInterval 允许您传递方法和毫秒数。您提供的方法将每隔您提供的毫秒数执行一次。因此,如果您想每 30 秒获取最新的推文,您可以这样调用:

setInterval(updateTweets,30000);

这将每 30 秒调用一次 updateTweets 方法,您可以在其中使用 ajax 来加载最新的推文。

有关 setInterval 的更多信息,您可以查看: http:// /www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

The javascript method setInterval allows you to pass a method and a number of milliseconds. The method you provide will be executed every number of milliseconds you provided. So if you wanted to grab the latest tweets every 30 seconds, you would call something like this:

setInterval(updateTweets,30000);

This would call the method updateTweets every thirty seconds, where you could use ajax to load up the latest tweets.

For more information on setInterval, you can check out: http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

瀞厅☆埖开 2024-10-18 19:00:41

最好的解决方案是在 PHP 中重新实现您的功能:

<?    
$url = "http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?";
$responseJsonString = file_get_contents($url);
$responseArray = json_decode($responseJsonString, $array=true);

// uncomment this to see what's in the response array:
// print_r($responseArray);
// Now, you can do as you like with $responseArray

然后通过 crontab 执行 PHP 脚本。

The best solution is to re-implement your functionality in PHP:

<?    
$url = "http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?";
$responseJsonString = file_get_contents($url);
$responseArray = json_decode($responseJsonString, $array=true);

// uncomment this to see what's in the response array:
// print_r($responseArray);
// Now, you can do as you like with $responseArray

And then execute the PHP script via crontab.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文