
December 9th, 2012, 10:31 AM
|
|
Contributing User
|
|
Join Date: Aug 2004
Posts: 229
Time spent in forums: 2 Days 14 h 19 m 9 sec
Reputation 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?
|