<?php
#从ES中导出数据
#两个参数:
#url为从ES导出数据的访问路径,不同的数据路径不一样
#post_data为json格式,是request body。
function
export(
$url
,
$post_data
){
$ch
= curl_init ();
curl_setopt (
$ch
, CURLOPT_URL,
$url
);
curl_setopt (
$ch
, CURLOPT_CUSTOMREQUEST,
"POST"
);
curl_setopt (
$ch
, CURLOPT_HEADER, 0 );
curl_setopt (
$ch
, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt (
$ch
, CURLOPT_POSTFIELDS,
$post_data
);
$arr
=curl_exec(
$ch
);
curl_close(
$ch
);
return
json_decode(
$arr
,
'assoc'
);;
}
#把数组数据导入ES
#两个参数:
#
$url
为导入数据的具体位置 如:http:
#post_data 导入ES的数据数组
function
import(
$url
,
$post_data
)
{
$json
=json_encode(
$post_data
);
$ci
= curl_init();
curl_setopt(
$ci
, CURLOPT_PORT, 9200);
curl_setopt(
$ci
, CURLOPT_TIMEOUT, 2000);
curl_setopt(
$ci
, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$ci
, CURLOPT_FORBID_REUSE, 0);
curl_setopt(
$ci
, CURLOPT_CUSTOMREQUEST,
'PUT'
);
curl_setopt(
$ci
, CURLOPT_URL,
$url
);
curl_setopt(
$ci
, CURLOPT_POSTFIELDS,
$json
);
$response
= curl_exec(
$ci
);
unset(
$post_data
);
unset(
$json
);
curl_close(
$ci
);
}
?>