User Tools

Site Tools


lemmy-api-php

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lemmy-api-php [2023/06/27 14:28]
admin
lemmy-api-php [2023/06/28 14:55] (current)
neil
Line 11: Line 11:
 <code bash> <code bash>
 for i in 145 156 162; do ./​lemmy-api.php purgePerson $i; done for i in 145 156 162; do ./​lemmy-api.php purgePerson $i; done
 +</​code>​
 +and to work through all IDs from 170-240:
 +<code bash>
 +for i in `seq 170 240`; do ./​lemmy-api.php purgePerson $i; done 
 </​code>​ </​code>​
  
Line 16: Line 20:
 <code bash> <code bash>
 git clone https://​git.mckillop.org/​lemmy-api-php git clone https://​git.mckillop.org/​lemmy-api-php
 +</​code>​
 +
 +Made a copy of the config file and edit ''​config.php''​ to add your domain, username and password to get started.
 +
 +<code bash>
 +cd lemmy-api-php
 +cp config.example.php config.php
 </​code>​ </​code>​
  
  
 Here is some example code for a PHP command line tool that will let you purge local users. Here is some example code for a PHP command line tool that will let you purge local users.
 +
 +<code php config.php>​
 +<?php
 +$lemmy_domain = "";​ // e.g. lemmy.ml
 +$username = "";​
 +$password = "";​
 +$token_file = "​.lemmy-api-token";​ // this is where your token is cached, can be any location
 +?>
 +</​code>​
  
 <code php lemmy-api.php>​ <code php lemmy-api.php>​
 #​!/​usr/​bin/​php #​!/​usr/​bin/​php
 <?php <?php
-$lemmy_api_url = "https://​lemmy.glasgow.social/​api/​v3/";​ +require("config.php");
-$username = "​username";​ +
-$password = "​password";​ +
-$token_file = "​.lemmy-api-token";+
  
 $implemented_commands = array( $implemented_commands = array(
Line 77: Line 94:
                 echo "​Attempting to purge person $person_id...";​                 echo "​Attempting to purge person $person_id...";​
                 $data = json_decode(http_post($lemmy_api_url."​admin/​purge/​person",​ array('​auth'​ => $token, '​person_id'​=>​intval($person_id),​ '​reason'​=>​$reason)),​ true);                 $data = json_decode(http_post($lemmy_api_url."​admin/​purge/​person",​ array('​auth'​ => $token, '​person_id'​=>​intval($person_id),​ '​reason'​=>​$reason)),​ true);
 +                $status = "​ERROR";​
 +                if(!empty($data['​success'​]) and $data['​success'​] == 1)
 +                        $status = "​OK";​
 +                echo $status."​\n";​
                 break;                 break;
         case "​deletePost":​         case "​deletePost":​
Line 82: Line 103:
                 echo "​Attempting to delete post ID $post_id...";​                 echo "​Attempting to delete post ID $post_id...";​
                 $data = json_decode(http_post($lemmy_api_url."​post/​delete",​ array('​auth'​ => $token, '​post_id'​=>​intval($post_id),​ '​deleted'​=>​true)),​ true);                 $data = json_decode(http_post($lemmy_api_url."​post/​delete",​ array('​auth'​ => $token, '​post_id'​=>​intval($post_id),​ '​deleted'​=>​true)),​ true);
-                if(!empty($data['​post_view'​]) and $data['​post_view'​]['​post'​]['​deleted'​] == 1) { +                ​$status = "​ERROR";​ 
-                        ​echo "OK\n"; +                ​if(!empty($data['​post_view'​]) and $data['​post_view'​]['​post'​]['​deleted'​] == 1) 
-                ​} else { +                        ​$status = "​OK";​ 
-                        ​echo "ERROR\n"; +                echo $status."​\n";​
-                }+
                 break;                 break;
         default:         default:
Line 108: Line 128:
         return $result;         return $result;
 } }
 +
  
 ?> ?>
 </​code>​ </​code>​
  
-That should be enough to get you started using the API from the command line.  If I get time I'll put a more complete version of this with some other useful commands (like banPerson) on my repo.+That should be enough to get you started using the API from the command line.  If I get time I'll put a more complete version of this with some other useful commands (like banPerson) on my repo as I find a use for them.
  
 ===== Getting Local Lemmy Users ===== ===== Getting Local Lemmy Users =====
lemmy-api-php.1687872506.txt.gz · Last modified: 2023/06/27 14:28 by admin