<?php /** * solr 操作类[solr php client] * Created by PhpStorm. * User: Administrator * Date: 14-11-6 * Time: 下午3:42 */ class solr{ private $user; private $pwd; private $host; private $port; private $core; public function solr($config=array()){ $this->Init_Config($config); } private function Init_Config($config){ global $_G; if(empty($config)&& iN_array('solr',$_G['setting']['plugins']['available'])){ $config=$_G['cache']['plugin']['solr']; } if(!empty($config)&&is_array($config)){ foreach($config as $key=>$value){ if(!array_key_exists($key,get_class_vars(get_class($this)))){ continue; } $this->setOption($key,$value); } } } /** +------------------------------------------------------------------------ *设置类属性值函数 +------------------------------------------------------------------------ * @param mix $key * @param mix $value */ private function setOption($key,$value){ $key=strtolower($key); $this->$key=$value; } /** * 认证调用的字符串 * @return string */ private function Authentication(){ return $this->user.':'.$this->pwd; } /** * 请求的参数 * @param bool $Auth * @return string */ public function QueryUrl($Auth=false){ $url='http://'; $url.=$Auth?$this->Authentication().'@':''; $url.=$this->host.':'.$this->port.'/'.$this->core.'/'; return $url; } //生成搜索链接 private function generateSearchUrl( $servlet, $params = array()){ $searchurl=$this->QueryUrl(); $queryString = http_build_query($params, null, "&"); $queryString = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $queryString); return $searchurl . $servlet .'?'. $queryString; } private function curlPost($url, $post, $timeout=3, $charset='gb2312', $contentType = "") { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); $header[] = 'Host:'.parse_url($url, PHP_URL_HOST); $header[] = $contentType?$contentType:'Content-type: application/x-www-form-urlencoded;charset='.$charset; $header[] = 'Content-Length:'.strlen($post); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 设置超时限制防止死循环 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $contents = curl_exec($ch); curl_close($ch); return $contents; } /** * post提交solr更新 * @param $updatedata * @return mixed */ public function solrUpdate($updatedata=array(),$params=array()){ $queryurl=$this->generateSearchUrl('update',$params); $contentType = "Content-type:application/json"; $response = $this->curlPost($queryurl,$updatedata,3,'',$contentType); return $response; } /** * 搜索数据 * */ public function search($params=array()){ $searchUrl = $this->generateSearchUrl("select", $params); $contentType = "Content-type:application/json"; $response = $this->curlPost($searchUrl, '', 3, '', $contentType); return $response; } //对象转数组 private static function object2array($object = null){ $object = is_object($object)?get_object_vars($object):$object; $object = (!$object)?"x":$object; if(is_array($object)){ foreach ($object as $k=>$v){ if(is_object($v)) $object[$k] = self::object2array($v); } } return $object; } } ?>