Created
February 19, 2013 21:47
-
-
Save niklibrato/4990391 to your computer and use it in GitHub Desktop.
librato php example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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/metrics'; | |
| $username = 'USERID@SERVER'; | |
| $api_key = 'API_KEY-long-string-of-numbers-and-letters'; | |
| $curl = curl_init($url); | |
| $curl_post_data = array( | |
| "gauges" => array( | |
| array("name" => "php-example-0", "value" => "11"), | |
| array("name" => "php-example-0", "value" => "22", source => "server-3") | |
| ) | |
| ); | |
| $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 | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment