Skip to content

Instantly share code, notes, and snippets.

@HarryR
Created July 25, 2012 16:13
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarryR/3177007 to your computer and use it in GitHub Desktop.
Save HarryR/3177007 to your computer and use it in GitHub Desktop.
NewRelic API for PHP
<?php
class NewRelic_Error extends Exception {}
function NewRelic_Date(DateTime $date) {
return $date->format('Y-m-d') . 'T' . $date->format('H:i:s') . 'Z';
}
function NewRelic_Metrics2Array(SimpleXMLElement $result) {
$return = array();
foreach( $result->metric AS $metric ) {
$begin = (string)$metric['begin'];
$end = (string)$metric['end'];
$field = (string)$metric->field;
$return[$begin . '|' . $end] = $field;
}
return $return;
}
class NewRelic {
private $apiKey;
private $accountID;
const RPM_URL = 'https://rpm.newrelic.com';
const API_URL = 'https://api.newrelic.com';
public function __construct($apiKey, $accountID) {
$this->apiKey = $apiKey;
$this->accountID = (int) $accountID;
}
public function getApplications() {
return $this->apiCall(self::RPM_URL.'/accounts/'.$this->accountID.'/applications.xml');
}
public function getSummary($appID) {
return $this->apiCall(self::RPM_URL.'/accounts/'.$this->accountID.'/applications/'.$appID.'/threshold_values.xml');
}
public function listMetrics($appID) {
return $this->apiCall(self::API_URL.'/api/v1/applications/'.$appID.'/metrics.xml');
}
public function getData($appID, DateTime $begin, DateTime $end, $metrics, $field, $summary = FALSE) {
$data = array(
'metrics' => $metrics,
'begin' => NewRelic_Date($begin),
'end' => NewRelic_Date($end),
);
if( $field ) {
$data['field'] = $field;
}
if( $summary ) {
$data['summary'] = 1;
}
return $this->apiCall(self::API_URL.'/api/v1/accounts/'.$this->accountID.'/applications/'.$appID.'/data.xml', 'get', $data);
}
public function sendDeployment($appID, $user, $description, $changelog, $revision) {
$data = array(
'deployment[application_id]' => $appID,
'deployment[description]' => $description,
'deployment[changelog]' => $changelog,
'deployment[user]' => $user,
'deployment[revision]' => $revision,
);
return $this->apiCall('deployments.xml', 'post', $data);
}
private function apiCall($url, $method = 'get', array $data = array()) {
$method = strtolower($method);
if ($method != 'get' && $method != 'post') {
throw new IllegalArgumentException();
}
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else {
if( count($data) ) {
$url .= '?' . http_build_query($data);
}
}
// set authentication header
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-api-key: '.$this->apiKey));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
if( empty($output) ) {
throw new NewRelic_Error("Empty response");
}
return simplexml_load_string($output);
}
}
@iiro
Copy link

iiro commented Dec 22, 2013

Hi

and thank you for this little script!

I´m having some problems with the deployment notification - I guess I´m doing something really wrong here...

I´m trying to do this:

<?php

include("lib/newrelic.php");

$relic = new NewRelic("apikeyhere", "accounidhere");

$appID="11111111";
$description="First test";
$changelog="testing";
$user="Myself";
$revision="0.01";



$relic->sendDeployment($appID, $user, $description, $changelog, $revision);

?>

but when I try to run this I just get an "empty response".

I have tried with "list applications" - the connection to the API works.

Can you help me?

@zyphlar
Copy link

zyphlar commented Jul 21, 2014

@iiro - it looks like there's a missing bit of url for the deployments bit on line 68:

<?php
//... line 68...
return $this->apiCall('deployments.xml', 'post', $data);

should be

<?php
//... line 68...
return $this->apiCall(self::API_URL.'/deployments.xml', 'post', $data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment