ElasticSearch搜索curl检测是否可用

function get($url,$rerror = 0,$ip = '',$header = []){
    $ch = curl_init();
    if(stripos($url,"https://")!==FALSE){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    if($header){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);//解决等待时间过长导致请求中断
    if($ip){
        $tmparr = parse_url($url);
        $resolve = [$tmparr['host'].':'.($tmparr['scheme']=='https'?'443':'80').':'.$ip];
        curl_setopt($ch, CURLOPT_RESOLVE, $resolve);
    }


    //默认使用iPv4
    if(defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')){
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    }
    $sContent = curl_exec($ch);
    var_dump($sContent);
    $aStatus = curl_getinfo($ch);
    var_dump($aStatus);
    curl_close($ch);
    if(intval($aStatus["http_code"])==200){
        return $sContent;
    }else{
        if($rerror){
            return [
                'error'=>'返回为空',
                'info'=>$aStatus
            ];
        }else{
            return false;
        }
    }
}

try{
    $header = [];
    $header[] =  "Authorization: Basic ".base64_encode("user:password"); //添加头,在name和pass处填写对应账号密码
    $content = get('http://127.0.0.1:9200',0,'',$header);
    print_r($content);
}catch(Exception $e){
    var_dump($e->getMessage());
}

  

posted @ 2021-12-11 16:52  飞鹰之歌  阅读(117)  评论(0)    收藏  举报