PHP Example

Here’s an example of how to get data into Librato using PHP:

 #!/usr/bin/php

## If the credentials are wrong or missing:
#HTTP Status Code: 401{"errors":{"request":["Authorization Required"]}}
## If there is something wrong with the data being sent you might get an error like this
#HTTP Status Code: 400{"errors":{"request":["Failed to parse JSON body: can't convert String into Hash"]}}
## If it completed successfully you will only see the status code
#HTTP Status Code: 200

<?php
$url      = 'https://metrics-api.librato.com/v1/measurements';
$username = 'USERID@SERVER';
$api_key  = 'API_KEY-long-string-of-numbers-and-letters';

$curl = curl_init($url);
$curl_post_data = array(
  "measurements" => array(
   array("name" => "php-example-0", "value" => "22", "tags" => array("test" => "test-1"))
   )
);

$headers = array(
    'Content-Type: application/json'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data));

curl_setopt($curl, CURLOPT_USERPWD, "$username:$api_key");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
## Show the payload of the POST
#print_r($curl_post_data);
$result = curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo "HTTP Status Code: " . $http_status;
echo $result
?>

Make sure you check out the PHP language bindings as well.