#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    317
    Rep Power
    0

    Running Mozrepl with PHP .... some last questions


    As MozRepl creates a server on port 4242, we can easily connect to it via PHP and send commands to it. A small example is given below which you we can run from the command line.

    what is aimed:

    well i want to fetch a number of pages - as thumbnails
    well - we can setup MozRepl for Perl and for PHP too.


    here some snippets for php

    The following code sends 3 commands to the MozRepl server and prints the responses.


    again: what is aimed:

    well i want to fetch a number of pages - as thumbnails
    i have a number of pages - stored in a txt-file

    example:

    PHP Code:
    www.google.com 
    www
    .altavista.com
    www
    .yahoo.com
    www
    .msm.com
    www
    .oracle.com 
    and so on....

    See the socketHelper class that does the dirt work


    PHP Code:
    <?php
     
    require_once('socketHelper.php');
     
    /* Commands to send to  MozRepl */
     
    $commands=<<<EOD
    repl.whereAmI()
    content.location.href = 'http://google.com'
    repl.quit()
    EOD;
     
    $firefox_socket = new SocketHelper;
    if(!
    $firefox_socket->connect()) exit;
     
    foreach(
    explode("\n",$commands) as $command){
        if(
    $command=='')continue; //Skip blank lines
        
    echo $firefox_socket->send_command($command);
    }
     
    ?>

    the Code for socketHelper class


    PHP Code:
    <?php
     
    /* socketHelper.php */
     
    class SocketHelper
    {
     
        private 
    $address "127.0.0.1";
        private 
    $port    "4242";
        private 
    $socket  null;
     
        public function 
    connect()
        {
            
    $this->socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
            if(!
    $this->socket){
                
    socket_strerror($this->socket)."\n";
                return 
    false;
            }
     
            
    $result=socket_connect($this->socket,$this->address,$this->port);
     
            if(!
    $result){
                
    socket_strerror($result)."\n";
                
    socket_close($this->socket);
                return 
    false;
            }
     
            
    $this->read();
            return 
    true;
        }
     
        
    /** Send a command to MozRepl */
        
    public function send_command($command){
            
    $command.="\n";
            
    socket_write($this->socket,$command);
            return 
    $this->read();
        }
     
     
        
    /* 
            Read from the Socket until we get a "repl>" prompt, 
            or loop forever.
         */
        
    private function read(){
            
    $response '';
            while(
    1){
                
    $chunk socket_read($this->socket,65536,PHP_BINARY_READ);
                if(
    $chunk === false){
                    echo 
    "Error reading from socket\n";
                    break;
                }
                if(
    $chunk === "") break; //No more data
     
                
    if(preg_match('|^(.*)\s*repl\d*>\s*$|s',$chunk,$match)){
                    
    $response .= $match[1];
                    break;
                }
     
                
    $response .= $chunk;
            }
            return 
    $response;
        }
    }
    ?>
    any hint for the idea to gather the thumbmails?
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1945
    If you post others code, please add proper credit to the owner of the source (http://www.codediesel.com/tools/peeking-inside-firefox-using-mozrepl/)

    Comments on this post

    • ptr2void agrees : metabo, copying and pasting code? Say it ain't so!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    317
    Rep Power
    0
    hello dear MrFujin

    many thanks for the hints

    Originally Posted by MrFujin
    If you post others code, please add proper credit to the owner of the source (http://www.codksediesel.com/tools/peeking-inside-firefox-using-mozrepl/)
    thx - thats true

    and for the rest of the question - i think i got the solution.
    welll - and now i want to set up the mozrepl propperly...


    ptr2void -close but no ciguar - but guessing that
    you did nt get it

IMN logo majestic logo threadwatch logo seochat tools logo