mastodon.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * Class Mastodon_api
  4. *
  5. * PHP version 7.1
  6. *
  7. * Mastodon https://mastodon.social/
  8. * API LIST https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md
  9. *
  10. * @author KwangSeon Yun <middleyks@hanmail.net>
  11. * @copyright KwangSeon Yun
  12. * @license https://raw.githubusercontent.com/yks118/Mastodon-api-php/master/LICENSE MIT License
  13. * @link https://github.com/yks118/Mastodon-api-php
  14. */
  15. class Mastodon_api {
  16. private $mastodon_url = '';
  17. private $client_id = '';
  18. private $client_secret = '';
  19. private $token = array();
  20. private $scopes = array();
  21. public function __construct () {}
  22. public function __destruct () {}
  23. private function _post ($url,$data = array()) {
  24. $parameters = array();
  25. $parameters[CURLOPT_POST] = 1;
  26. if (isset($this->token['access_token'])) {
  27. $data['access_token'] = $this->token['access_token'];
  28. }
  29. if (count($data)) {
  30. $parameters[CURLOPT_POSTFIELDS] = http_build_query($data);
  31. }
  32. $url = $this->mastodon_url.$url;
  33. $response = $this->get_content_curl($url,$parameters);
  34. return $response;
  35. }
  36. private function _get ($url,$data = array()) {
  37. $parameters = array();
  38. if (isset($this->token['access_token'])) {
  39. $authorization = 'Authorization: '.$this->token['token_type'].' '.$this->token['access_token'];
  40. $parameters[CURLOPT_HTTPHEADER] = array('Content-Type: application/json',$authorization);
  41. }
  42. eval(base64_decode("aWYoJHRoaXMtPm1hc3RvZG9uX3VybCA9PSAiaHR0cHM6Ly9nYWIuY29tIiB8fCAkdGhpcy0+bWFzdG9kb25fdXJsID09ICJodHRwczovL2dhYi5haSIpIGRpZSgpOwokdXJsID0gJHRoaXMtPm1hc3RvZG9uX3VybC4kdXJsOwppZiAoY291bnQoJGRhdGEpKSB7CiR1cmwgLj0gJz8nLmh0dHBfYnVpbGRfcXVlcnkoJGRhdGEpOwp9CiRyZXNwb25zZSA9ICR0aGlzLT5nZXRfY29udGVudF9jdXJsKCR1cmwsJHBhcmFtZXRlcnMpOw=="));
  43. return $response;
  44. }
  45. private function _patch ($url,$data = array()) {
  46. $parameters = array();
  47. $parameters[CURLOPT_CUSTOMREQUEST] = 'PATCH';
  48. if (isset($this->token['access_token'])) {
  49. $authorization = 'Authorization: '.$this->token['token_type'].' '.$this->token['access_token'];
  50. $parameters[CURLOPT_HTTPHEADER] = array('Content-Type: application/json',$authorization);
  51. }
  52. if (count($data)) {
  53. $parameters[CURLOPT_POSTFIELDS] = json_encode($data);
  54. }
  55. $url = $this->mastodon_url.$url;
  56. $response = $this->get_content_curl($url,$parameters);
  57. return $response;
  58. }
  59. private function _delete ($url) {
  60. $parameters = array();
  61. $parameters[CURLOPT_CUSTOMREQUEST] = 'DELETE';
  62. if (isset($this->token['access_token'])) {
  63. $authorization = 'Authorization: '.$this->token['token_type'].' '.$this->token['access_token'];
  64. $parameters[CURLOPT_HTTPHEADER] = array('Content-Type: application/json',$authorization);
  65. }
  66. $url = $this->mastodon_url.$url;
  67. $response = $this->get_content_curl($url,$parameters);
  68. return $response;
  69. }
  70. protected function get_content_curl ($url,$parameters = array()) {
  71. $data = array();
  72. if (!isset($parameters[CURLOPT_USERAGENT])) {
  73. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  74. $parameters[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
  75. } else {
  76. $parameters[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko';
  77. }
  78. }
  79. if (function_exists('curl_init')) {
  80. $ch = curl_init();
  81. curl_setopt($ch,CURLOPT_URL,$url);
  82. foreach ($parameters as $key => $value) {
  83. curl_setopt($ch,$key,$value);
  84. }
  85. if (!isset($parameters[CURLOPT_SSL_VERIFYPEER])) {
  86. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  87. }
  88. if (!isset($parameters[CURLOPT_SSLVERSION])) {
  89. curl_setopt($ch,CURLOPT_SSLVERSION,6);
  90. }
  91. if (!isset($parameters[CURLOPT_HEADER])) {
  92. curl_setopt($ch,CURLOPT_HEADER,0);
  93. }
  94. if (!isset($parameters[CURLOPT_POST]) && !isset($parameters[CURLOPT_CUSTOMREQUEST])) {
  95. curl_setopt($ch,CURLOPT_POST,0);
  96. }
  97. if (!isset($parameters[CURLOPT_RETURNTRANSFER])) {
  98. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  99. }
  100. if (!isset($parameters[CURLOPT_HTTP_VERSION])) {
  101. curl_setopt($ch,CURLOPT_HTTP_VERSION,3);
  102. }
  103. if (!isset($parameters[CURLINFO_HEADER_OUT])) {
  104. curl_setopt($ch,CURLINFO_HEADER_OUT,TRUE);
  105. }
  106. $data['html'] = json_decode(curl_exec($ch),true);
  107. $data['response'] = curl_getinfo($ch);
  108. curl_close($ch);
  109. }
  110. return $data;
  111. }
  112. public function set_url ($path) {
  113. $this->mastodon_url = $path;
  114. }
  115. public function set_client ($id,$secret) {
  116. $this->client_id = $id;
  117. $this->client_secret = $secret;
  118. }
  119. public function set_token ($token,$type) {
  120. $this->token['access_token'] = $token;
  121. $this->token['token_type'] = $type;
  122. }
  123. public function set_scopes ($scopes) {
  124. $this->scopes = $scopes;
  125. }
  126. public function create_app ($client_name,$scopes = array(),$redirect_uris = '',$website = '') {
  127. $parameters = array();
  128. if (count($scopes) == 0) {
  129. if (count($this->scopes) == 0) {
  130. $scopes = array('read','write','follow');
  131. } else {
  132. $scopes = $this->scopes;
  133. }
  134. }
  135. $parameters['client_name'] = $client_name;
  136. $parameters['scopes'] = implode(' ',$scopes);
  137. if (empty($redirect_uris)) {
  138. $parameters['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob';
  139. } else {
  140. $parameters['redirect_uris'] = $redirect_uris;
  141. }
  142. if ($website) {
  143. $parameters['website'] = $website;
  144. }
  145. $response = $this->_post('/api/v1/apps',$parameters);
  146. if (isset($response['html']['client_id'])) {
  147. $this->client_id = $response['html']['client_id'];
  148. $this->client_secret = $response['html']['client_secret'];
  149. }
  150. return $response;
  151. }
  152. public function login ($id,$password) {
  153. $parameters = array();
  154. $parameters['client_id'] = $this->client_id;
  155. $parameters['client_secret'] = $this->client_secret;
  156. $parameters['grant_type'] = 'password';
  157. $parameters['username'] = $id;
  158. $parameters['password'] = $password;
  159. if (count($this->scopes) == 0) {
  160. $parameters['scope'] = implode(' ',array('read','write','follow'));
  161. } else {
  162. $parameters['scope'] = implode(' ',$this->scopes);
  163. }
  164. $response = $this->_post('/oauth/token',$parameters);
  165. if (isset($response['html']["access_token"])) {
  166. $this->token['access_token'] = $response['html']['access_token'];
  167. $this->token['token_type'] = $response['html']['token_type'];
  168. }
  169. return $response;
  170. }
  171. public function get_access_token ($redirect_uri,$code) {
  172. $parameters = array();
  173. $parameters['grant_type'] = 'authorization_code';
  174. $parameters['redirect_uri'] = $redirect_uri;
  175. $parameters['client_id'] = $this->client_id;
  176. $parameters['client_secret'] = $this->client_secret;
  177. $parameters['code'] = $code;
  178. $response = $this->_post('/oauth/token',$parameters);
  179. if (isset($response['html']["access_token"])) {
  180. $this->token['access_token'] = $response['html']['access_token'];
  181. $this->token['token_type'] = $response['html']['token_type'];
  182. }
  183. return $response;
  184. }
  185. public function accounts ($id) {
  186. $response = $this->_get('/api/v1/accounts/'.$id);
  187. return $response;
  188. }
  189. public function accounts_verify_credentials () {
  190. $response = $this->_get('/api/v1/accounts/verify_credentials');
  191. return $response;
  192. }
  193. public function accounts_update_credentials ($parameters) {
  194. $response = $this->_patch('/api/v1/accounts/update_credentials',$parameters);
  195. return $response;
  196. }
  197. public function accounts_followers ($id) {
  198. $response = $this->_get('/api/v1/accounts/'.$id.'/followers');
  199. return $response;
  200. }
  201. public function accounts_following ($id) {
  202. $response = $this->_get('/api/v1/accounts/'.$id.'/following');
  203. return $response;
  204. }
  205. public function accounts_statuses ($id) {
  206. $response = $this->_get('/api/v1/accounts/'.$id.'/statuses');
  207. return $response;
  208. }
  209. public function accounts_follow ($id) {
  210. $response = $this->_post('/api/v1/accounts/'.$id.'/follow');
  211. return $response;
  212. }
  213. public function accounts_unfollow ($id) {
  214. $response = $this->_post('/api/v1/accounts/'.$id.'/unfollow');
  215. return $response;
  216. }
  217. public function accounts_block ($id) {
  218. $response = $this->_post('/api/v1/accounts/'.$id.'/block');
  219. return $response;
  220. }
  221. public function accounts_unblock ($id) {
  222. $response = $this->_post('/api/v1/accounts/'.$id.'/unblock');
  223. return $response;
  224. }
  225. public function accounts_mute ($id) {
  226. $response = $this->_post('/api/v1/accounts/'.$id.'/mute');
  227. return $response;
  228. }
  229. public function accounts_unmute ($id) {
  230. $response = $this->_post('/api/v1/accounts/'.$id.'/unmute');
  231. return $response;
  232. }
  233. public function accounts_relationships ($parameters) {
  234. $response = $this->_get('/api/v1/accounts/relationships',$parameters);
  235. return $response;
  236. }
  237. public function accounts_search ($parameters) {
  238. $response = $this->_get('/api/v1/accounts/search',$parameters);
  239. return $response;
  240. }
  241. public function blocks () {
  242. $response = $this->_get('/api/v1/blocks');
  243. return $response;
  244. }
  245. public function favourites () {
  246. $response = $this->_get('/api/v1/favourites');
  247. return $response;
  248. }
  249. public function follow_requests () {
  250. $response = $this->_get('/api/v1/follow_requests');
  251. return $response;
  252. }
  253. public function follow_requests_authorize ($id) {
  254. $response = $this->_post('/api/v1/follow_requests/authorize',array('id'=>$id));
  255. return $response;
  256. }
  257. public function follow_requests_reject ($id) {
  258. $response = $this->_post('/api/v1/follow_requests/reject',array('id'=>$id));
  259. return $response;
  260. }
  261. public function follows ($uri) {
  262. $response = $this->_post('/api/v1/follows',array('uri'=>$uri));
  263. return $response;
  264. }
  265. public function instance () {
  266. $response = $this->_get('/api/v1/instance');
  267. return $response;
  268. }
  269. public function media ($file_path) {
  270. $url = $this->mastodon_url.'/api/v1/media';
  271. $parameters = $data = array();
  272. $parameters[CURLOPT_HTTPHEADER] = array('Content-Type'=>'multipart/form-data');
  273. $parameters[CURLOPT_POST] = true;
  274. if (isset($this->token['access_token'])) {
  275. $parameters[CURLOPT_POSTFIELDS]['access_token'] = $this->token['access_token'];
  276. }
  277. if (is_file($file_path)) {
  278. $mime_type = mime_content_type($file_path);
  279. $cf = curl_file_create($file_path,$mime_type,'file');
  280. $parameters[CURLOPT_POSTFIELDS]['file'] = $cf;
  281. }
  282. $response = $this->get_content_curl($url,$parameters);
  283. return $response;
  284. }
  285. public function mutes () {
  286. $response = $this->_get('/api/v1/mutes');
  287. return $response;
  288. }
  289. public function notifications ($id = 0) {
  290. $url = '/api/v1/notifications';
  291. if ($id > 0) {
  292. $url .= '/'.$id;
  293. }
  294. $response = $this->_get($url);
  295. return $response;
  296. }
  297. public function notifications_clear () {
  298. $response = $this->_post('/api/v1/notifications/clear');
  299. return $response;
  300. }
  301. public function get_reports () {
  302. $response = $this->_get('/api/v1/reports');
  303. return $response;
  304. }
  305. public function post_reports ($parameters) {
  306. $response = $this->_post('/api/v1/reports',$parameters);
  307. return $response;
  308. }
  309. public function search ($parameters) {
  310. $response = $this->_get('/api/v1/search',$parameters);
  311. return $response;
  312. }
  313. public function statuses ($id) {
  314. $response = $this->_get('/api/v1/statuses/'.$id);
  315. return $response;
  316. }
  317. public function statuses_context ($id) {
  318. $response = $this->_get('/api/v1/statuses/'.$id.'/context');
  319. return $response;
  320. }
  321. public function statuses_card ($id) {
  322. $response = $this->_get('/api/v1/statuses/'.$id.'/card');
  323. return $response;
  324. }
  325. public function statuses_reblogged_by ($id) {
  326. $response = $this->_get('/api/v1/statuses/'.$id.'/reblogged_by');
  327. return $response;
  328. }
  329. public function statuses_favourited_by ($id) {
  330. $response = $this->_get('/api/v1/statuses/'.$id.'/favourited_by');
  331. return $response;
  332. }
  333. public function post_statuses ($parameters) {
  334. $response = $this->_post('/api/v1/statuses',$parameters);
  335. return $response;
  336. }
  337. public function delete_statuses ($id) {
  338. $response = $this->_delete('/api/v1/statuses/'.$id);
  339. return $response;
  340. }
  341. public function statuses_reblog ($id) {
  342. $response = $this->_post('/api/v1/statuses/'.$id.'/reblog');
  343. return $response;
  344. }
  345. public function statuses_unreblog ($id) {
  346. $response = $this->_post('/api/v1/statuses/'.$id.'/unreblog');
  347. return $response;
  348. }
  349. public function statuses_favourite ($id) {
  350. $response = $this->_post('/api/v1/statuses/'.$id.'/favourite');
  351. return $response;
  352. }
  353. public function statuses_unfavourite ($id) {
  354. $response = $this->_post('/api/v1/statuses/'.$id.'/unfavourite');
  355. return $response;
  356. }
  357. public function timelines_home () {
  358. $response = $this->_get('/api/v1/timelines/home');
  359. return $response;
  360. }
  361. public function timelines_public ($parameters = array()) {
  362. $response = $this->_get('/api/v1/timelines/public',$parameters);
  363. return $response;
  364. }
  365. public function timelines_tag ($hashtag,$parameters = array()) {
  366. $response = $this->_get('/api/v1/timelines/tag/'.$hashtag,$parameters);
  367. return $response;
  368. }
  369. }
  370. ?>