mastodon.php 12 KB

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