PHP code example of scaleplan / php-mattermost-driver

1. Go to this page and download the library: Download scaleplan/php-mattermost-driver 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/ */

    

scaleplan / php-mattermost-driver example snippets


 use \Scaleplan\Mattermost\Driver;
 
 $container = new \Pimple\Container([
     'driver' => [
         'url' => 'your_chat_url',
         'login_id' => 'your_login_id',
         'password' => 'your_password',
     ],
     'guzzle' => [
         //put here any options for Guzzle
     ]
 ]);
 
 $driver = new Driver($container);
 $result = $driver->authenticate();
 

 use \Scaleplan\Mattermost\Driver;
 
 $container = new \Pimple\Container([
     'driver' => [
         'url' => 'your_chat_url',
         'token' => 'your_token',
     ],
     'guzzle' => [
         //put here any options for Guzzle
     ]
 ]);
 
 $driver = new Driver($container);
 $result = $driver->authenticate();
 

if ($result->getStatusCode() == 200) {
    echo "Everything is ok.";
    var_dump(json_decode($result->getBody()));
} else {
    echo "HTTP ERROR " . $result->getStatusCode();
}


//Add a new user
$result = $driver->getUserModel()->createUser([
    'email'    => '[email protected]', 
    'username' => 'test', 
    'password' => 'testpsw'
]);

//Get a user
$result = $driver->getUserModel()->getUserByUsername('username');

//Please read the UserModel class or refer to the api documentation for a complete list of available methods.

//Create a channel
$result = $driver->getChannelModel()->createChannel([
    'name'         => 'new_channel',
    'display_name' => 'New Channel',
    'type'         => 'O',
]);


//Get a channel
$result = $driver->getChannelModel()->getChannelByName('team_id_of_the_channel_to_return', 'new_channel');

//Please read the ChannelModel class or refer to the api documentation for a complete list of available methods.

//Create a post
$result = $driver->getPostModel()->createPost([
    'channel_id' => 'The channel ID to post in',
    'messages' => 'The message contents, can be formatted with Markdown',
]);


//Get a post
$result = $driver->getPostModel()->getPost('post_id_of_the_post_to_return');

//Please read the PostModel class or refer to the api documentation for a complete list of available methods.

//Upload a file
$result = $driver->getFileModel()->uploadFile([
    'channel_id' => 'The ID of the channel that this file will be uploaded to',
    'filename' => 'The name of the file to be uploaded',
    'files' => fopen('Path of the file to be uploaded', 'rb'),
]);

//Send a post with the file just uploaded
$result = $driver->getPostModel()->createPost([
    'channel_id' => 'The channel ID to post in',
    'messages' => 'The message contents, can be formatted with Markdown',
    'file_ids' => 'A list of file IDs to associate with the post',
]);

//Please read the FileModel class or refer to the api documentation for a complete list of available methods.

//Get a list of the user's preferences
$result = $driver->getPreferenceModel('user_id')->getUserPreference();

//Please read the PreferenceModel class or refer to the api documentation for a complete list of available methods.

composer 

composer