Perché secondo voi questo streaming si blocca?

alessio85

Nuovo Utente
23 Nov 2012
7
0
0
il codice è il seguente...Si connette ad un api twitter che restituisce un flusso dei tweet circolanti in esso.. e vorrei che girasse per quanto dico io... ma dopo un po di tempo... circa mezz'ora dentro $fp non trovo più niente (io lo vorrei far girare per qualche giorno)... c' è qualcuno che mi sa spiegare il perché??
Forse esiste una funzione migliore x connettersi ad un flusso di dati in streaming...se fosse me la potete suggerire???
Un grazie infinito a chi risponderà...

PHP:
$fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);
            $ini=time();
            $fine=time();
            $passati=$fine-$ini;
            if (!$fp)
            {
                die ("ERROR: Twitter Stream Error: failed to open socket");
            } else
            {
                //
                // build the request
                //https://'.'stream.twitter.com/1/statuses/sample.json
                $request  = "GET /1/statuses/sample.json";
                $request .= " HTTP/1.1\r\n";
                $request .= "Host: stream.twitter.com\r\n";
                $request .= "Authorization: Basic ";
                $request .= base64_encode($this->m_username . ':' . $this->m_password);
                $request .= "\r\n\r\n";

                //
                // write the request
                //
                fwrite($fp, $request);

                //
                // set it to non-blocking
                //
                stream_set_blocking($fp, 0);
                //(!feof($fp))&&
                while(($passati<($this->durata)))
                {   
                    $fine=time();
                    $passati=$fine-$ini;
                    
                    $read   = array($fp);
                    $write  = null;
                    $except = null;
                    $res = stream_select($read, $write, $except, 600, 0);
                    if ( ($res === false) || ($res == 0) )
                    {
                        break;
                    }

                    //
                    // read the JSON object from the socket
                    //
                    $json = fgets($fp);
                    if ( ($json !== false) && (strlen($json) > 0) )
                    {
                        //
                        // decode the socket to a PHP array
                        //
                        $data = json_decode($json, true);
                        if ($data)
                        {
                            //
                            // process it
                            //
                            $this->process_tweet($data);
                        }
                    }
                }
            }
 
Ultima modifica:
ciao
il problema può essere che gli script hanno un tempo di vita e puo essere che lo script si arresti prima di terminare
quindi prova a settare il tempo di vita per quello che ti serve.
poi vedo una cosa che non mi quadra, scrivi:
PHP:
<?php
//....
	$ini=time();
	$fine=time();
	$passati=$fine-$ini;
//...
?>
messi cosi in prtatica hai $ini uguale a $fine per cui $passati == 0, probabilmente devi spostare la fine e la sottrazione dopo le operazioni che gli fai fare
 
ciao e grazie x la risposta...
però prima di quel codice (che non ho riportato) c'è il seguente comando set_time_limit(0); il che non dovrebbe far terminare lo script...
inoltre $ini uguale a $fine per cui $passati == 0 è bruttino è vero ma tanto non è un problema perchè tanto 0 sarà minore della $durata e quindi non mi crea problemi...
uffa questo script mi sta facendo impazzire da una settimana ....
 

Discussioni simili