<?php
function zabbix_sender( $server,$port,$hostname,$data ) {
   $body = (object)array(
      "request" => "sender data",
      "data" => array(),
   );

   foreach( $data as $key => $value ) {
      array_push( $body->data, (object)array(
            "host" => $hostname,
            "key" => $key,
            "value" => $value,
         )
      );
   }

   $json_body = json_encode( $body );

   $size = strlen( $json_body );
   $request = pack( "a4CV2a*", "ZBXD", 1, $size, ( $size >> 32 ), $json_body );

   // Suppress warning, since we check below for success
   @$s=fsockopen($server,$port,$errnum,$errstr,15);
   if( is_resource( $s ) ) {
      fputs($s,$request);
      // 13 byte header
      for( $bytesRead = 0; $bytesRead < 13 && !feof( $s ); $bytesRead++ ) {
         $header .= fread( $s, 1 );
      }

      list( $ZBX, , $sizeL, $sizeH ) =
         array_values( unpack( 'a4zbx/Csep/Vh/Vl', $header ) );
      $msgSize = ( $sizeL + ( $sizeH >> 32 ) );
      while( !feof( $s ) ) {
         $ret .= fread( $s, $msgSize );
      }

      if( $retStatus = json_decode($ret,true) ) {
         $status = preg_split( "/([A-Z][a-z ]*)\s+(\S+)\s*/",
            $retStatus["info"], -1,
            PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
         for( $i = 0; $i < count( $status ); $i += 2 ) {
            $key = $status[$i];
            $val = $status[$i+1];
            $retStatus["status"][$key] = $val;
         }
      }
      else {
         $retStatus = array(
            "response" => "failed",
            "info" => "Invalid response",
         );
      }
      return( $retStatus );
   } else {
      $retStatus = array(
         "response" => "failed",
         "info" => "$errstr ($errno)",
      );
      return( $retStatus );
   }
}

$data = array(
   "statusthing1" => "175",
   "statusthing2" => "1231"
);

$rv = zabbix_sender( "127.0.0.1", 10051, "myhostname", $data );
var_dump( $rv );

// $rv sollte etwa so aussehen
// array(3) {
//   ["response"]=>
//   string(7) "success"
//   ["info"]=>
//   string(51) "Processed 2 Failed 0 Total 2 Seconds spent 0.000041"
//   ["status"]=>
//   array(4) {
//     ["Processed"]=>
//     string(1) "2"
//     ["Failed"]=>
//     string(1) "0"
//     ["Total"]=>
//     string(1) "2"
//     ["Seconds spent"]=>
//     string(8) "0.000041"
//   }
// }

?>