thecodingcompany / php-mastodon
Mastodon PHP 库 OAuth 和 API
v1.0.5
2017-04-26 18:49 UTC
Requires
- php: >=5.6.0
This package is not auto-updated.
Last update: 2024-09-15 03:29:13 UTC
README
Mastodon REST API 的 PHP 库
包含什么?
- 应用创建。
- 完整的 oAuth 实现,通过用户授权你的应用。
- 创建并获取授权令牌、访问令牌、客户端 ID、客户端密钥和承载令牌。
- 用户认证
- 获取用户信息
- 获取用户关注者和被关注者
- 获取用户状态
- 发布状态更新
使用 Composer 安装
composer require thecodingcompany/php-mastodon
有疑问和示例?
是的,请给我发邮件至:vangelier at hotmail dot com 在 #Twitter @digital_human 上联系我 在 #Mastodon https://mastodon.social/@digitalhuman
开始使用
步骤 1
第一步是创建一个所谓的应用。这个应用代表你的“服务”。通过这个应用,你为用户提供服务或出于其他原因使用 Mastodon。
创建应用的步骤非常简单
<?php
/**
* Intellectual Property of #Mastodon
*
* @copyright (c) 2017, #Mastodon
* @author V.A. (Victor) Angelier <victor@thecodingcompany.se>
* @version 1.0
* @license https://apache.ac.cn/licenses/GPL-compatibility.html GPL
*
*/
require_once("autoload.php");
$t = new \theCodingCompany\Mastodon();
/**
* Create a new App and get the client_id and client_secret
*/
$token_info = $t->createApp("MyCoolAppName", "http://www.internet.com");
$serializedData = serialize($token_info);
// 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
?>
参数 $token_info
现在包含你的 'client_id' 和 'client_secret'。这些信息在你的一生中都很重要;()。将其存储在文件、数据库或数组中。每次与 Mastodon 通信时都需要这些信息。
步骤 2
现在你(你的应用)希望向用户提供服务。为此,用户需要授权你的应用。否则,你无法帮助他们。为此,你需要将用户重定向到 Mastodon,并使用你的令牌请求权限。以下是一个示例
<?php
/**
* Intellectual Property of #Mastodon
*
* @copyright (c) 2017, #Mastodon
* @author V.A. (Victor) Angelier <victor@thecodingcompany.se>
* @version 1.0
* @license https://apache.ac.cn/licenses/GPL-compatibility.html GPL
*
*/
require_once("autoload.php");
$recoveredData = file_get_contents('mastodon_creds');
// unserializing to get actual array
$recoveredArray = unserialize($recoveredData);
$t = new \theCodingCompany\Mastodon();
/**
* 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;
步骤 3
所以你现在有 3 个令牌。客户端 ID、客户端密钥和用户的访问令牌。现在将访问令牌交换为承载令牌,你就可以完成了。保存这些令牌!
<?php
/**
* Intellectual Property of #Mastodon
*
* @copyright (c) 2017, #Mastodon
* @author V.A. (Victor) Angelier <victor@thecodingcompany.se>
* @version 1.0
* @license https://apache.ac.cn/licenses/GPL-compatibility.html GPL
*
*/
require_once("autoload.php");
$recoveredData = file_get_contents('mastodon_creds');
// unserializing to get actual array
$recoveredArray = unserialize($recoveredData);
$t = new \theCodingCompany\Mastodon();
/**
* 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!
*
*/
步骤 4
然后要发布状态,你只需这样做
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)
$t->postStatus('API Test - PLZ ignore <3');
完整代码概述和选项等
<?php
/**
* Intellectual Property of #Mastodon
*
* @copyright (c) 2017, #Mastodon
* @author V.A. (Victor) Angelier <victor@thecodingcompany.se>
* @version 1.0
* @license https://apache.ac.cn/licenses/GPL-compatibility.html GPL
*
*/
require_once("autoload.php");
$t = new \theCodingCompany\Mastodon();
/**
* Create a new App and get the client_id and client_secret
*/
$token_info = $t->createApp("MyCoolAppName", "http://www.internet.com");
//Get the authorization url
$auth_url = $t->getAuthUrl();
/*
* 1) Send the above URL '$auth_url' to the user. The need to authorize your App.
* 2) When they authorized your app, they will receive a token. The authorization token.
* 3) Put the authorization token in the request below to exchange it for a bearer token.
*/
//Request the bearer token
$token_info = $t->getAccessToken("7c47d0c636314a1dff21reryyy5edf91884856dc0f78148f848d475136");
/**
* The above '$token_info' will now be an array with the info like below. (If successfull)
* No these are not real, your right.
*
{
"client_id": "87885c2bf1a9d9845345345318d1eeeb1e48bb676aa747d3216adb96f07",
"client_secret": "a1284899df5250bd345345f5fb971a5af5c520ca2c3e4ce10c203f81c6",
"bearer": "77e0daa7f252941ae8343543653454f4de8ca7ae087caec4ba85a363d5e08de0d"
}
*/
/**
* Authenticate a user by username and password and receive the bearer token
*/
$bearer_token = $t->authUser("vangelier@hotmail.com", "MySecretP@ssW0rd");
/**
* Get the userinfo by authentication
*/
$user_info = $t->getUser("vangelier@hotmail.com", "MySecretP@ssW0rd");
/**
* Get user followers / following
*/
$followers = $t->authenticate("vangelier@hotmail.com", "MySecretP@ssW0rd")
->getFollowers();
/**
* Get user statusses
*/
$statusses = $t->authenticate("vangelier@hotmail.com", "MySecretP@ssW0rd")
->getStatuses();
/**
* Post status update
*/
$status = $t->authenticate("vangelier@hotmail.com", "MySecretP@ssW0rd")
->postStatus("Text status update");