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!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...