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

    Join Date
    Aug 2004
    Posts
    307
    Rep Power
    0

    Poking in FireFox with PHP & Mozrepl - a example


    dear community,

    found a mozrepl examples that run with PHP

    http://www.codediesel.com/tools/peeking-inside-firefox-using-mozrepl/

    what can i do - note i ve installed all the stuff on a opensuse 12.2


    i tried it out - with the code shown here:
    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);
    }
     
    ?>
    and the following one


    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;
        }
    }
    ?>
    see the results that i got back in terminal


    PHP Code:
    martin@linux-wyee:~> cd php
    martin
    @linux-wyee:~/phpphp moz1.php
    PHP Fatal error
    :  Call to undefined function socket_create() in /home/martin/php/socketHelper.php on line 14
    martin
    @linux-wyee:~/php
    what can i do - note i ve installed all the stuff on a opensuse 12.2


    can you advice

    look forward

    meta
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    socket_create isn't available to you. Are you running on a shared host which may have disabled it? Or are you running on PHP4 or earlier?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo