* Full oAuth implementation to authorize your App by users.
* Create and get authorization token, access token, client_id, client_secret and bearer token.
* Authenticate users
* Get user information
* Get user followers and following
* Get user status
* Post status update
## Installation using Composer
```
composer require thecodingcompany/php-mastodon
```
## Questions and example?
Yes, mail me at: vangelier at hotmail dot com
Contact me on #Twitter @digital_human
Contact me on #Mastodon https://mastodon.social/@digitalhuman
## Get started
### Step 1
First step is you need to create a so called App. This app represents your 'service'. With this app you provide services to users or use Mastodon for other reasons.
// save the special tokens to a file, so you don't lose them
file_put_contents('mastodon_creds',$serializedData);// this will save it in the same folder as this file
?>
```
The parameter ```$token_info``` now has your 'client_id' and 'client_secret'. This information is important for the rest of your life ;). Store it in a file, DB or array. You need this everytime you communicate with Mastodon.
### Step 2
Now you (your app) wants to provide services to a user. For this the user needs to authorize your app. Else you can't help him/her. To do this you need to redirect the user, with your tokens to Mastodon and ask for permission so to say. And example:
* We now have a client_id and client_secret. Set the domain and provide the library with your App's client_id and secret.
*/
$t->setMastodonDomain("mastodon.social");// Set the mastodon domain, you can remove this line if you're using mastodon.social as it's the default
$t->setCredentials($recoveredArray);// use the keys from the file we stored in Step 1
/**
* Now that is set we can get the Authorization URL and redirect the user to Mastodon
* After the user approves your App, it will return with an Access Token.
*/
$auth_url=$t->getAuthUrl();
header("Location: {$auth_url}",true);
exit;
```
### Step 3
So you now have 3 tokens. The client_id, client_secret and the users access_token. Now exchange the access token for a bearer token and you are done. Save these tokens!
* We now have a client_id and client_secret. Set the domain and provide the library with your App's client_id and secret.
*/
$t->setMastodonDomain("mastodon.social");// Set the mastodon domain, you can remove this line if you're using mastodon.social as it's the default
$t->setCredentials(recoveredArray);// use the keys from the file we stored in Step 1
$token_info=$t->getAccessToken("7c47d0c636314a1dff21reryyy5edf91884856dc0f78148f848d475136");//The access token you received in step 2 from the user.
/**
* The above '$token_info' will now give you a bearer token (If successfull), you also need to store that and keep it safe!
*
*/
```
## Step 4
To then post a status, you just do this:
```
require_once("autoload.php");
$t = new \theCodingCompany\Mastodon();
$t->setMastodonDomain(website address); // change this to whatever Mastodon instance you're using, or remove it entirely if you're using mastodon.social (as it's the default)
$t->setCredentials($credentials); // where $credentials are your "client_id", "client_secret" and "bearer" in the form of an array with those exact names (from what you got in the earlier steps)