fluent-logger-phpを使わずPHPのcURLだけでログを送信したときのメモ
Composerで入れた fluent-logger-php を使って TreasureData にログを送信したときは port: 24224
に送信していたけど
直接curlコマンドやPHPのcURLを使うときは port: 8888
に送信するらしい。
cURL を使った時
/**
* TDデータ送信
*/
$td_logs_array = array();
$td_logs_array['uid'] = $_COOKIE['uid'];
$td_logs_array['hoge'] = $_GET['hoge'];
$td_logs_array['ip'] = $_SERVER['REMOTE_ADDR'];
$td_logs_array['ua'] = $_SERVER['HTTP_USER_AGENT'];
$json_data = json_encode($td_logs_array);
$ch = curl_init('http://td-agent-server:8888/td.hoge_logs.fuga_log');
$defaults = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array("json" => $json_data)
);
curl_setopt_array($ch, $defaults);
curl_exec($ch);
curl_close($ch);
file_get_contents を使った時
$td_logs_array = array();
$td_logs_array['uid'] = $_COOKIE['uid'];
$td_logs_array['hoge'] = $_GET['hoge'];
$td_logs_array['ip'] = $_SERVER['REMOTE_ADDR'];
$td_logs_array['ua'] = $_SERVER['HTTP_USER_AGENT'];
//POSTデータ
$data = array(
"json" => json_encode($td_logs_array)
);
$data = http_build_query($data, "", "&");
//header
$header = array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: ".strlen($data)
);
$context = array(
"http" => array(
"method" => "POST",
"header" => implode("\r\n", $header),
"content" => $data
)
);
$url = "http://td-agent-server:8888/td.hoge_logs.fuga_log";
echo file_get_contents($url, false, stream_context_create($context));
覇王色を求めて file_get_contentsでPOSTデータ送信を参考にしました。