smartgage.de Docs

curl with PHP Script

write data with curl


// https://docs.influxdata.com/influxdb/v1.7/guides/writing_data/
// https://docs.influxdata.com/influxdb/v1.7/tools/api/#write-http-endpoint

define('INFLUX_USERNAME','username');
define('INFLUX_PASSWORD','*********');
define('INFLUX_WRITE','https://www.smartgage.de:8086/write');

$db='mydatabase';
$url = INFLUX_WRITE . "?db=$db&precision=ms";
$body ="
MeinInfluxProjekt,SN=12345678,Standort=Hennigsdorf field_key1=1.0000,field_key2=1.0101,field_key3=1.0234 1561306371000\n
MeinInfluxProjekt,SN=12345678,Standort=Hennigsdorf field_key1=1.1000,field_key2=1.1101,field_key3=1.0234 1561306372000";
$headers = array(
    'Content-Type: application/x-www-form-urlencoded',
    'Accept: application/json'
);
// open connection
$ch = curl_init();

// set options
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, INFLUX_USERNAME . ":" . INFLUX_PASSWORD);
curl_setopt($ch,CURLOPT_POSTFIELDS, $body);

// execute
	/* $result is empty if writing was successful or is a json string with error message like:
	{"error":"unable to parse 'test2,tag1=12345678 ch1=1.0a,ch2=2,02': invalid number"} */

$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
print "http_status = $http_status
\n"; if ($result === false) { // throw new Exception('Curl error: ' . curl_error($crl)); print_r('curl error: ' . curl_error($ch)); } // Close cURL session handle curl_close($ch);