php 用 webkitd client

webkitdのphp用のクライアントを作る

<?php

class webkitd_client{

  var $socket;
  var $debug;

  function __construct($host='127.0.0.1',$port=1982){
     $this->__open($host,$port);
  }

  function __destruct(){
    $this->close();
  }

  function __call($name,$args=array()){
    return call_user_func_array( array($this,'req'),array(strtr($name,'_','-'),$args[0]));
  }

  private function __open($host,$port){
    $this->socket = fsockopen($host,$port);
    if($this->socket === false) { die("conection error"); }
  }

  public function req($type,$data){
    $req = array(
      'type'=>$type,
      'data'=>$data,
    );
    $json_req = json_encode($req)."\r\n";
    if($this->debug){echo $json_req;};
    fwrite($this->socket,$json_req);
    $line = fgets($this->socket);
    if($this->debug){echo $line;}
    return $line;
  }

  public function status(){
    $this->server(array('command'=>'status'));
  }

  public function close(){
    $this->server(array('command'=>'disconnect'));
    $this->__close();
  }

  private function __close(){
    fclose($this->socket);
  }


}

$wc = new webkitd_client();
$wc->debug=true;;
print_r($wc->req('load-url',array('url'=>'http://www.yahoo.co.jp/','timeout'=>10)));
exit;
print_r($wc->load_url(array('url'=>'http://www.yahoo.co.jp/','timeout'=>10)));