From: Neil Date: Thu, 3 Dec 2020 13:15:32 +0000 (+0000) Subject: Initial commit X-Git-Url: https://git.mckillop.org/gitweb/?a=commitdiff_plain;h=f7641d24f5b86b3886351dd94a4ee373e1a790cd;p=lemmy-php Initial commit --- f7641d24f5b86b3886351dd94a4ee373e1a790cd diff --git a/lemmy.class.php b/lemmy.class.php new file mode 100644 index 0000000..704de56 --- /dev/null +++ b/lemmy.class.php @@ -0,0 +1,61 @@ +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; + } +} + +?>