我怎样才能“停下来”呢?从javaScript创建一个PHP页面——意思是模仿关闭浏览器窗口的行为


How can I "stop" a PHP page from javaScript - meaning mimic the behaviour of closing browser window

PHP和javaScript新手。

我放了一些代码,通过流API挖掘推文。

实际上,我正在调整由Mike Pultz制作的代码(粘贴在下面)。(谢谢!)

工作正常。它由两个PHP代码组成。

-一个是类实际挖掘。它处理oAuth,打开连接,收听tweet,并将它们吐在页面上(现在是最后一部分…作为测试)。它有一个while(1)无限循环。

-另一个构造前者的实例,并调用启动一切的函数,将oAuth秘密作为参数传递。

到目前为止,我一直在调用它(第二个脚本)直接从浏览器。当我关闭窗口(或在浏览器中点击停止加载按钮)时,与twitter的连接关闭(循环停止)。这很好。虽然我真的不明白为什么会发生这种情况,一旦循环在另一个脚本中…我假设实例在页面结束时被销毁,是这样吗?

好,现在我继续,开始写一些js来建立一个接口,所有这些。我将有一个函数调用php启动脚本,传递要搜索的参数。我还想要一个函数来停止流。

问题:如何在PHP中实现这一点?我认为在重新创建矿工对象,不调用启动器函数…但这并不是一个很好的方法。

调用代码:

<?php
require 'ctwitter_stream2.php';
$t = new ctwitter_stream2();
$t->login($_cKey,
 $_cSecret, 
 $_aToken,
 $_aSecret);
$t->start(array('query'));
?>

挖掘类(已经从原来的调整):

<?php
class ctwitter_stream2
{
    private $m_oauth_consumer_key;
    private $m_oauth_consumer_secret;
    private $m_oauth_token;
    private $m_oauth_token_secret;
    private $m_oauth_nonce;
    private $m_oauth_signature;
    private $m_oauth_signature_method = 'HMAC-SHA1';
    private $m_oauth_timestamp;
    private $m_oauth_version = '1.0';
    public function __construct()
    {
        //
        // set a time limit to unlimited
        //
        set_time_limit(0);
    }
    //
    // set the login details
    //
    public function login($_consumer_key, $_consumer_secret, $_token, $_token_secret)
    {
        $this->m_oauth_consumer_key     = $_consumer_key;
        $this->m_oauth_consumer_secret  = $_consumer_secret;
        $this->m_oauth_token            = $_token;
        $this->m_oauth_token_secret     = $_token_secret;
        //
        // generate a nonce; we're just using a random md5() hash here.
        //
        $this->m_oauth_nonce = md5(mt_rand());
        return true;
    }
    //
    // process a tweet object from the stream
    //
    private function process_tweet( $_data)
    {
        print_r($_data);
        return true;
    }
    //
    // the main stream manager
    //
    public function start(array $_keywords)
    {
        while(1)
        {
            $fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);
            if (!$fp)
            {
                echo "ERROR: Twitter Stream Error: failed to open socket";
            } else
            {
                //
                // build the data and store it so we can get a length
                //
                $data = 'track=' . rawurlencode(implode($_keywords, ','));
                //
                // store the current timestamp
                //
                $this->m_oauth_timestamp = time();
                //
                // generate the base string based on all the data
                //
                $base_string = 'POST&' . 
                    rawurlencode('https://stream.twitter.com/1.1/statuses/filter.json') . '&' .
                    rawurlencode('oauth_consumer_key=' . $this->m_oauth_consumer_key . '&' .
                        'oauth_nonce=' . $this->m_oauth_nonce . '&' .
                        'oauth_signature_method=' . $this->m_oauth_signature_method . '&' . 
                        'oauth_timestamp=' . $this->m_oauth_timestamp . '&' .
                        'oauth_token=' . $this->m_oauth_token . '&' .
                        'oauth_version=' . $this->m_oauth_version . '&' .
                        $data);
                //
                // generate the secret key to use to hash
                //
                $secret = rawurlencode($this->m_oauth_consumer_secret) . '&' . 
                    rawurlencode($this->m_oauth_token_secret);
                //
                // generate the signature using HMAC-SHA1
                //
                // hash_hmac() requires PHP >= 5.1.2 or PECL hash >= 1.1
                //
                $raw_hash = hash_hmac('sha1', $base_string, $secret, true);
                //
                // base64 then urlencode the raw hash
                //
                $this->m_oauth_signature = rawurlencode(base64_encode($raw_hash));
                //
                // build the OAuth Authorization header
                //
                $oauth = 'OAuth oauth_consumer_key="' . $this->m_oauth_consumer_key . '", ' .
                        'oauth_nonce="' . $this->m_oauth_nonce . '", ' .
                        'oauth_signature="' . $this->m_oauth_signature . '", ' .
                        'oauth_signature_method="' . $this->m_oauth_signature_method . '", ' .
                        'oauth_timestamp="' . $this->m_oauth_timestamp . '", ' .
                        'oauth_token="' . $this->m_oauth_token . '", ' .
                        'oauth_version="' . $this->m_oauth_version . '"';
                //
                // build the request
                //
                $request  = "POST /1.1/statuses/filter.json HTTP/1.1'r'n";
                $request .= "Host: stream.twitter.com'r'n";
                $request .= "Authorization: " . $oauth . "'r'n";
                $request .= "Content-Length: " . strlen($data) . "'r'n";
                $request .= "Content-Type: application/x-www-form-urlencoded'r'n'r'n";
                $request .= $data;
                //
                // write the request
                //
                fwrite($fp, $request);
                //
                // set it to non-blocking
                //
                stream_set_blocking($fp, 0);
                while(!feof($fp))
                {
                    $read   = array($fp);
                    $write  = null;
                    $except = null;
                    //
                    // select, waiting up to 10 minutes for a tweet; if we don't get one, then
                    // then reconnect, because it's possible something went wrong.
                    //
                    $res = stream_select($read, $write, $except, 600, 0);
                    if ( ($res == false) || ($res == 0) )
                    {
                        break;
                    }
                    //
                    // read the JSON object from the socket
                    //
                    $json = fgets($fp);
                    //
                    // look for a HTTP response code
                    //
                    if (strncmp($json, 'HTTP/1.1', 8) == 0)
                    {
                        $json = trim($json);
                        if ($json != 'HTTP/1.1 200 OK')
                        {
                            echo 'ERROR: ' . $json . "'n";
                            return false;
                        }
                    }
                    //
                    // if there is some data, then process it
                    //
                    if ( ($json !== false) && (strlen($json) > 0) )
                    {
                        //
                        // decode the socket to a PHP array
                        //
                            //
                            // process it
                            //
                            $this->process_tweet($json);

                    }
                }
            }
            fclose($fp); 
            sleep(10);
        }
        return;
    }
};
?>

假设你想从Javascript中停止php代码,我想为你提出一个解决方案。

让我们假设您已经启动了脚本,一段时间后,您希望停止脚本

1)你可以通过AJAX ping一个php页面,并传递一个参数,将设置保存到文件中。

2)在做流挖掘的php代码中,当你可以在文件

中存在一个值

假设AJAX ping成功保存了该值,while循环将拦截它并执行一个中断,该中断将成功退出循环,从而停止它

希望有帮助!