Initial commit master
authorNeil <neil@0xm.net>
Thu, 3 Dec 2020 13:15:32 +0000 (13:15 +0000)
committerNeil <neil@0xm.net>
Thu, 3 Dec 2020 13:15:32 +0000 (13:15 +0000)
lemmy.class.php [new file with mode: 0644]

diff --git a/lemmy.class.php b/lemmy.class.php
new file mode 100644 (file)
index 0000000..704de56
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+
+class lemmy {
+       function __construct($lemmy_url) {
+               $this->url = $lemmy_url."/api/v1";
+               $this->last_error = null;
+       }
+
+       function registerUser($data) {
+               $response = $this->http_request("/user/register", $data);
+               return $response;
+       }
+
+       function login($username, $password) {
+               $details = array("username_or_email" => $username,
+                               "password"=>$password);
+               $response = $this->http_request("/user/login", $details);
+               if(!$response)
+                       return false;
+               return $response['jwt'];
+       }
+
+       function createPost($data) {
+               if(empty($data['name']) and empty($data['url'])) {
+                       $this->last_error = "Missing name/url";
+                       return false;
+               }
+               if(empty($data['community_id']) or empty($data['auth'])) {
+                       $this->last_error = "Missing community_id or auth token";
+                       return false;
+               }
+               if(empty($data['nsfw']))
+                       $data['nsfw'] = false;
+               $response = $this->http_request("/post", $data);
+               if(!$response)
+                       return false;
+               return $response['post'];
+       }
+
+       function http_request($url, $payload) {
+               $payload = json_encode($payload);
+               print_r($payload);
+               $ch = curl_init($this->url.$url);
+               echo "Trying for ".$this->url.$url."\n";
+               curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+               curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
+               curl_setopt($ch, CURLOPT_POST, true);
+               curl_setopt($ch, CURLINFO_HEADER_OUT, true);
+               curl_setopt($ch, CURLOPT_HTTPHEADER, array(     'Content-Type: application/json',
+                                                               'Content-Length: '.strlen($payload)));
+               $response = json_decode(curl_exec($ch), true);
+               print_r($response);
+               if(!empty($response['error'])) {
+                       $this->last_error = $response['error'];
+                       return false;
+               }
+               return $response;
+       }
+}
+
+?>