郁闷
最近需要用到php查询elk中的数据,怎奈yii2中使用composer安装扩展很费力,费时,另一个项目用的是thinkphp,服务器php版本是5.6无法使用php-ElasticSearch,为了方便,干脆来个最简单的原生请求吧
/**
* 最简易版 使用curl请求ElasticSearch
* @param $url '请求地址 ip:port:index'
* @param $body '请求的json'
* @param $auth '用户名:密码'
* @param $is_ssl '处理https,如果访问的网站是https,需要设置为true'
* @return mixed
*/
function curl_get($url, $body, $auth, $is_ssl = false)
{
$curl = curl_init();
$headers = [];
$headers[] = "Content-Type:application/json";
$postBody = json_encode($body);
curl_setopt($curl, CURLOPT_URL, $url);//设置url
if ($is_ssl) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//检查服务器SSL证书中是否存在一个公用名(common name)。
}
curl_setopt($curl, CURLOPT_USERPWD, $auth);//设置用户名密码
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头
curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);//设置请求体
$html = curl_exec($curl);
return $html;
}
$url = 'http://ip:port/{index}/_search';
$auth = 'username:password';//用户名:密码
$body = [
"query" => [
'match_phrase' => [
'field' => 'value'
]
],
];
exit((curl_get($url, $body, $auth)));
更多精彩
免费共享资源
http://www.ltchao.top