Httpcws.php 1.38 KB
<?php

class Q_Ws_Adapter_Httpcws extends Q_Ws_Adapter_Abstract
{

    private $server;

    public function __construct(array $options)
    {
        $_options = array(
            'mode' => Q_Server_Core::SERVER_MODE_PROXY,
            'select' => Q_Server_Core::SERVER_SELECT_MODE_RAND
        );
        $options = $_options + $options;
        $this->server = Q_Server::factory('search', 'words', 'servers', $options);
    }

    /**
     * (non-PHPdoc)
     * @see Q_Ws_Adapter_Abstract::getWords()
     * @return array
     */
    public function getWords($inputs)
    {
        if (strlen($inputs) <= 20480) {
            $words = addslashes($inputs);
            try {
                $inputs = iconv('UTF-8', 'GBK//TRANSLIT', $words);
            } catch (Exception $e) {
                return array($words);
            }
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->server['host'] . ':' . $this->server['port']);
            curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($inputs));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            $result = iconv('GBK', 'UTF-8//TRANSLIT', curl_exec($ch));
            curl_close($ch);
            $result = empty($result) ? array() : array_filter(explode(' ', $result));
            array_push($result, $words);
            return $result;
        } else {
            return array();
        }
    }
}