我如何 "stop" 来自 javaScript 的 PHP 页面 - 意思是模仿关闭浏览器的行为 window

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

对 PHP 和 javaScript 非常陌生。

我发布了一些代码,用于挖掘推文、流媒体 API。

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

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

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

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

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

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

问题: 我怎样才能在 PHP 中做到这一点?我想在重新创建矿工对象时不要调用启动函数...但这并不是一个很好的方法。

调用代码:

<?php
require 'ctwitter_stream2.php';

$t = new ctwitter_stream2();

$t->login($_cKey,
 $_cSecret, 
 $_aToken,
 $_aSecret);

$t->start(array('query'));
?>

挖矿class(已经从原来的调整):

<?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 循环将拦截它并执行 break 以成功退出循环,从而停止它

希望对您有所帮助!