dongqingcheng2903 2013-03-05 21:44
浏览 82
已采纳

运行一个不确定的PHP脚本一个小时。 每小时重启一次

I wrote a PHP script to pull tweets from the Twitter firehose and store them into a database. Ideally I want to just let it run so that it collects tweets over time, thus, it's wrapped in a while(1) loop.

This seems to be problematic because it's timing out. If I just run it in a browser, it won't run for more than 30 seconds before timing out and giving me a 324 Error.

Question: Is there a way that I can have it run for a certain amount of time (20 seconds), auto kill itself, then restart? All in a cron job (PS...I don't know how to write a cron job)?

Background: Site hosted on Godaddy. Would ideally like to run this on my hosting server there.

The Script:

<?php
    $start = time();
    $expAddress = "HOSTNAME";
    $expUser = "USERNAME";
    $expPwd = "PASSWORD";
    $database = "DBNAME";

    $opts = array(
        'http' => array(
            'method'    =>  "POST",
            'content'   =>  'keywords,go,here',
        )
    );

    // Open connection to stream
    $db = mysql_connect($expAddress, $expUser, $expPwd);
    mysql_select_db($database, $db);

    $context = stream_context_create($opts);
    while (1) {
        $instream = fopen('https://USERNAME:PASSWORD@stream.twitter.com/1/statuses/filter.json','r' ,false, $context);
        while(! feof($instream)) {

             if(time() - $start > 5) { // break after 5 seconds
                break;
             }


            if(! ($line = stream_get_line($instream, 100000, "
"))) {
                continue;
            }
            else {
                $tweet = json_decode($line);

                // Clean before storing             

                            // LOTS OF VARIABLES FOR BELOW...REMOVED FOR READABILITY

                // Send to database
                $ok = mysql_query("INSERT INTO tweets 
                    (created_at, from_user, from_user_id, latitude, longitude, tweet_id, language_code, 
                            place_name, profile_img_url, source, text, retweet_count, followers_count,
                            friends_count, listed_count, favorites_count) 
                    VALUES 
                    (NOW(), '$from_user', '$from_user_id', '$latitude', '$longitude', '$tweet_id', '$language_code', 
                            '$place_name', '$profile_img_url', '$source', '$text', '$retweet_count', '$followers_count',
                            '$friends_count', '$listed_count', '$favorites_count')");

                if (!$ok) { echo "Mysql Error: ".mysql_error(); }

                flush();
            }
        }
    }
?>
  • 写回答

3条回答 默认 最新

  • dtbam62840 2013-03-05 22:00
    关注

    If I get well your need, the best thing for you is to use cron job making a script run indefinitely will not be a good idea.

    As specifier in one of you comments you are using a hosting server Godaddy so probably you will not be able to have shell access, BUT depending on your cPanel version you may be able to create and define cron job.

    see this link and this google search

    Perhaps, if you don't have this option and you are wiling to let a browser opened I would suggest the following

    create an html page as a client which would make an ajax request every hours to your PHP script, like this you emulate a cron job function

    the ajax request code might look like (using jQuery)

    function makeRequest(){
        $.ajax({
            url: "http://yourhost/url-to-your-script.php",
            complete: function(data){
                setTimeout(function(){
                    makeRequest();
                }, 60 * 60 * 1000); // Minutes * Seconds * MS
            }
        });
    }
    makeRequest();
    

    I hope this helps

    EDIT

    this link might help too

    IMPORTANT DO NOT FORGET TO REMOVE THE INFINITE LOOP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度