PHP code example of fundevogel / php-mastodon

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

    

fundevogel / php-mastodon example snippets




undevogel\Mastodon\Api;

# Initialize Api for given instance (defaults to 'mastodon.social')
$api = new Api('freiburg.social');

# Generate access token via ..
# (1) .. OAuth call or ..
$api->accessToken = $api->oauth()->token('cl13nt_1d', 'cl13nt_s3cr3t')['access_token'];

# (2) .. app creation (create an app, get one `access_token` for free!)
$api->accessToken = 'ur-t0t4lly-s3cr3t-p4ssw@rd';

# If you want to obtain an authorization code, and want to know where to get one ..
$url = $api->getAuthURL('cl13nt_1d');

# .. but you might want to provide what you have & use the login helper
$api->logIn();

# This helper takes the authorization code as parameter - however,
# if you provided client key / secret / access token,
# it will attempt to provide whatever access level is possible

# Fetch statuses of given account
foreach ($api->accounts()->statuses('106612343490709443') as $status) {
    echo $status->content();
}

# Note: In case you obtained login-level access, you may omit the ID parameter, which gives back your own account's statuses, like so:
foreach ($api->accounts()->statuses() as $status) {
    echo $status->content();
}

# Fetch followers
foreach ($api->accounts()->followers('106612343490709443') as $follower) {
    echo $follower->displayName();
}

# View your timelines
$timelines = $api->timelines();

# (1) Public timeline
var_dump($timelines->public());

# (2) Home timeline (= accounts you are following)
var_dump($timelines->home());

# Fetch information about your instance
var_dump($api->instance()->get());


# ... enjoy playing with Mastodon's API!
text
composer