PHP code example of jc21 / slack-notifications

1. Go to this page and download the library: Download jc21/slack-notifications 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/ */

    

jc21 / slack-notifications example snippets




use jc21\SlackNotification;
use jc21\SlackAttachment;

$webhookUrl = 'your incoming webhook url';
$iconURl    = '';

// Create Attachment first
$attachment = new SlackAttachment();
$attachment->setFallback('Someone has tested the slack-notifications code successfully');
$attachment->setPretext('<https://github.com/jc21/slack-notifications|slack-notifications> was tested');
$attachment->setColor('#00ce3a');
$attachment->setAuthorName('Jamie Curnow');
$attachment->setAuthorLink('http://jc21.com');
$attachment->setAuthorIcon('https://avatars2.githubusercontent.com/u/1518257?v=3&s=460');

// Fields of the Attachment
$attachment->addField('Description', 'slack-notifications is a PHP package to help you out', false);
$attachment->addField('Type',        'Test',                                                 true);
$attachment->addField('Result',      'Success',                                              true);

// The notification, which uses the Attachment
$notification = new SlackNotification($webhookUrl);
$notification->setChannel('#general');  // Use @ for a username. ie: @jc21
$notification->setUsername('slack-notifications');
$notification->setIconUrl($iconUrl);
$notification->addAttachment($attachment);

try {
    $notification->send();
} catch (\Exception $e) {
    print 'Error: ' . $e->getMessage() . PHP_EOL;
    exit(1);
}
bash
# Install Composer
curl -sS https://getcomposer.org/installer | php