PHP code example of samlitowitz / php-new-relic-event-api-client

1. Go to this page and download the library: Download samlitowitz/php-new-relic-event-api-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

samlitowitz / php-new-relic-event-api-client example snippets


interface Client {
	public function send(PhpNewRelic\CustomEventCollection $customEvents): PhpNewRelic\EventAPI\Http\Response;
}



use PhpNewRelic\CustomEvent;
use PhpNewRelic\CustomEventCollection;
use PhpNewRelic\EventAPI\Http\Client;
use PhpNewRelic\EventAPI\Http\DomainName;
use PhpNewRelic\EventAPI\Http\Exception\GZipEncodingFailedException;
use PhpNewRelic\EventAPI\Http\Exception\PayloadExceedsMaximumSizeException;
use PhpNewRelic\EventAPI\Http\Exception\SubmissionErrorException;

define('NEW_RELIC_ACCOUNT_ID', '<YOUR-NEW-RELIC-ACCOUNT-ID>');
define('NEW_RELIC_API_KEY', '<YOUR-NEW-RELIC-API-KEY>');

// Instantiate the client
$client = new Client(NEW_RELIC_ACCOUNT_ID, NEW_RELIC_API_KEY, DomainName::US);

// Create custom event(s)
$events = CustomEventCollection::fromArray([
	CustomEvent::fromArray(
		'yourCustomEventType',
		[
			new CustomEvent\Attribute(
				new CustomEvent\Attribute\Name('stringAttr'),
				new CustomEvent\Attribute\Value\String_('string')
			),
			new CustomEvent\Attribute(
				new CustomEvent\Attribute\Name('floatAttr'),
				new CustomEvent\Attribute\Value\Float_(2.1)
			),
			new CustomEvent\Attribute(
				new CustomEvent\Attribute\Name('intAttr'),
				new CustomEvent\Attribute\Value\Integer(2)
			),
		]
	),
	// ...
]);
try {
	$response = $client->send($events);
} catch (GZipEncodingFailedException $e) {
	// Handle a failure to encode here
} catch (PayloadExceedsMaximumSizeException $e) {
	// Handle a too large payload side here
} catch (SubmissionErrorException $e) {
	// Handle submission errors here
} catch (Throwable $t) {
	// Handle other throws here
}