Getting Data In

How to post to Http Event Collector with PHP?

daniel333
Builder

All,

Anyone ever post to HEC using PHP? Got a working example? Or see where I am going wrong?

<?php
        $ch = curl_init("https://10.x.x.x:8088/services/collector/event");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERPWD, "x:MYTOKEN");
        curl_setopt($ch, CURLOPT_POSTFIELDS, "number=10");
        $output = curl_exec($ch);
        curl_close($ch);
        echo $output;
?>
0 Karma
1 Solution

daniel333
Builder

Got it! Trick was the formatting of the $data variable.

// Send Data to HEC with PHP

<?php
        $data = "{\"event\":\"testing\",\"sourcetype\": \"st\"}";
        $ch = curl_init("https://10.x.x.x:8088/services/collector/event");
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERPWD, "x:cbmytoen");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        echo $output;
?>

View solution in original post

daniel333
Builder

Got it! Trick was the formatting of the $data variable.

// Send Data to HEC with PHP

<?php
        $data = "{\"event\":\"testing\",\"sourcetype\": \"st\"}";
        $ch = curl_init("https://10.x.x.x:8088/services/collector/event");
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERPWD, "x:cbmytoen");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        echo $output;
?>
Get Updates on the Splunk Community!

Get the T-shirt to Prove You Survived Splunk University Bootcamp

As if Splunk University, in Las Vegas, in-person, with three days of bootcamps and labs weren’t enough, now ...

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Wondering How to Build Resiliency in the Cloud?

IT leaders are choosing Splunk Cloud as an ideal cloud transformation platform to drive business resilience,  ...