Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cogdog
Last active February 22, 2023 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cogdog/af963b0604e5fcaede72049131946c13 to your computer and use it in GitHub Desktop.
Save cogdog/af963b0604e5fcaede72049131946c13 to your computer and use it in GitHub Desktop.
Fetch Mastodon Mentions with PHP
<?php
// Mastodon account details, hardwired for dev, add yours as needed
$creds = [
'account' => '', //username in form @user@instance.social
'host' => '', // url without trailing slash e.g. https://instance.social
'url' => '', // user account URL https://instance.social/@user
'token' => '', // access token created in Edit Profile-> Development
'getcount' => 10 // number of posts to get,max=30
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mastodon Fetching for <?php echo $creds['account']?></title>
</head>
<body>
<h1>Fetching Mastodon Responses</h1>
<p>Save us from the claws of the Dark Lord of Twitter! Can I haz the most recent <?php echo $creds['getcount']?> mentions to <a href="<?php echo $creds['url']?>"><?php echo $creds['account']?></a>?</p>
<?php
$headers = [
'Authorization: Bearer ' . $creds['token']
];
// send the image using a cURL POST
$ch_media_status = curl_init();
curl_setopt($ch_media_status, CURLOPT_URL, $creds['host'] . '/api/v1/notifications?limit=' . $creds['getcount'] . '&types[]=mention');
curl_setopt($ch_media_status, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_media_status, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch_media_status, CURLOPT_POST, 0);
curl_setopt($ch_media_status, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch_media_status, CURLOPT_SSL_VERIFYPEER, false);
echo '<p><code>Sending request to ' . $creds['host'] . '...</code></p>';
$media_response = curl_exec($ch_media_status);
$toots = json_decode($media_response);
$show = true;
echo '<ol>';
foreach($toots as $toot) {
// parse tag names
$tags = [];
$toottags = $toot->status->tags;
foreach ($toottags as $thetag) {
$tags[] = $thetag->name;
}
echo '<li><strong>From</strong>: @'. $toot->account->username . '<br /><strong>status id</strong>: '. $toot->status->id . '<br /><strong>post time</strong> : ' . $toot->status->created_at .'<br /><strong>tags</strong> : '. implode(', ', $tags) . '<br /><strong>url</strong> : <a href="' . $toot->status->url . '">'. $toot->status->url . '</a><br><strong>content</strong> :' . strip_tags($toot->status->content) . '<br /><br /></li>';
}
echo '</ol>';
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment