123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- class ZhClient {
- var $mHost, $mPort, $mFP, $mConnected;
-
- function ZhClient($host, $port) {
- $this->mHost = $host;
- $this->mPort = $port;
- $this->mConnected = $this->connect();
- }
-
- function isconnected() {
- return $this->mConnected;
- }
-
- function connect() {
- wfSuppressWarnings();
- $errno = $errstr = '';
- $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
- wfRestoreWarnings();
- if(!$this->mFP) {
- return false;
- }
- return true;
- }
-
- function query($request) {
- if(!$this->mConnected)
- return false;
- fwrite($this->mFP, $request);
- $result=fgets($this->mFP, 1024);
- list($status, $len) = explode(" ", $result);
- if($status == 'ERROR') {
-
- print "zhdaemon error $len<br />\n";
- return false;
- }
- $bytesread=0;
- $data='';
- while(!feof($this->mFP) && $bytesread<$len) {
- $str= fread($this->mFP, $len-$bytesread);
- $bytesread += strlen($str);
- $data .= $str;
- }
-
- if(strlen($data) != $len)
- return false;
- return $data;
- }
-
- function convert($text, $tolang) {
- $len = strlen($text);
- $q = "CONV $tolang $len\n$text";
- $result = $this->query($q);
- if(!$result)
- $result = $text;
- return $result;
- }
-
- function convertToAllVariants($text) {
- $len = strlen($text);
- $q = "CONV ALL $len\n$text";
- $result = $this->query($q);
- if(!$result)
- return false;
- list($infoline, $data) = explode('|', $result, 2);
- $info = explode(";", $infoline);
- $ret = array();
- $i=0;
- foreach($info as $variant) {
- list($code, $len) = explode(' ', $variant);
- $ret[strtolower($code)] = substr($data, $i, $len);
- $i+=$len;
- }
- return $ret;
- }
-
- function segment($text) {
- $len = strlen($text);
- $q = "SEG $len\n$text";
- $result = $this->query($q);
- if(!$result) {
- $result = ZhClientFake::segment($text);
- }
- return $result;
- }
-
- function close() {
- fclose($this->mFP);
- }
- }
|