Recording deployments using a PHP script

You can send information about application deployments to New Relic via the REST API or via a PHP program or script. To record deployments via a script, you must have libcurl available for your PHP installation.

Customizing the script

Customize the example script to record deployments with your PHP application:

  1. Ensure that libcurl is installed in your PHP installation.
  2. Copy the example script below to a text editor.
  3. In the $apikey line, specify your New Relic API key.
  4. Specify the app for which you want New Relic to record a deployment:

    • Specify an existing New Relic app ID.

      OR

    • Specify an $app_name corresponding to an existing New Relic for PHP app. If you specify the app via its app name, ensure you uncomment the following line in the data string section of the script:

      $dep_dat = "deployment[app_name]=".$app_name;
  5. Optional: Customize the $dep_description, $dep_change, $dep_user, and $dep_rev values to record additional details about the deployment. If you customize these values, ensure you uncomment their lines in the data string section of the script. For example, to record user name in your script, uncomment this line of the data string section:

    $dep_dat = $dep_dat."&deployment[user]=".$dep_user;
  6. Save the script.
  7. Call the script during your deployment process.

Example script

Caution: This is a generic example and should be customized for your system configuration.


#!/usr/bin/php
<?php

#Deployment
# Assumes the availability of libcurl in the PHP environment.

#Make your changes here:

$apikey = "{INSERT YOUR API KEY HERE}";

#Specify an existing New Relic app name OR app ID
#$app_name = "{INSERT YOUR APPLICATION NAME HERE}";
$app_id = "{INSERT YOUR APPLICATION ID HERE}";

$dep_description = "This is your app id deployment";
#$dep_change = "This is a change log entry";
#$dep_user = "This is the user entry";
#$dep_rev = "This is a version number";

#compose the data string for curl

#$dep_dat = "deployment[app_name]=".$app_name;
$dep_dat = "deployment[app_id]=".$app_id;
$dep_dat = $dep_dat."&deployment[description]=".$dep_description;
#$dep_dat = $dep_dat."&deployment[changelog]=".$dep_change;
#$dep_dat = $dep_dat."&deployment[user]=".$dep_user;
#$dep_dat = $dep_dat."&deployment[revision]=".$dep_rev;

#There should be no changes necessary beyond this point

#deployment url at New Relic
$url = "https://api.newrelic.com/deployments.xml";

#Create header info
$header = array("x-api-key:".$apikey);

#initialize curl 
$ch = curl_init();

curl_setopt ($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt ($ch, CURLOPT_POSTFIELDS, $dep_dat );

# Make the curl call for deployment
$http_result = curl_exec ($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

#close curl 
curl_close ($ch);

#output status 
vprintf ("Code  %s\n", $http_code);
vprintf ("Results %s\n", $http_result);
if ($error) {
   vprintf ("Error %s\n",$error);
}

?>

For more help

Additional documentation resources include:

Join the discussion about PHP in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.