Skip to content

Instantly share code, notes, and snippets.

@esinanturan
Forked from Chak10/rapidgator_api.php
Created October 26, 2023 11:39
Show Gist options
  • Save esinanturan/0259d03a7c45abb8fdbb9fb65b1aa817 to your computer and use it in GitHub Desktop.
Save esinanturan/0259d03a7c45abb8fdbb9fb65b1aa817 to your computer and use it in GitHub Desktop.
Class based on Rapidgator Api
<?php
class Rg {
/*
Based on Rapidgator Api
https://support.rapidgator.net/index.php?/Knowledgebase/Article/View/89/9/do-you-have-api
*/
public $log_err, $down_err, $upl_err, $upl_id, $link;
private $sid, $logged;
public function login($email, $pass) {
$data = array(
"username" => $email,
"password" => $pass
);
if (!file_exists("rg_sid.key")) {
$login = json_decode($this->post("https://rapidgator.net/api/user/login", $data));
file_put_contents("rg_sid.key", json_encode($login));
} else {
$login = json_decode(file_get_contents("rg_sid.key"));
}
if ($this->user_info($login->response->session_id)->response_status != 200) {
$login = json_decode($this->post("https://rapidgator.net/api/user/login", $data));
file_put_contents("rg_sid.key", json_encode($login));
}
if ($login->response_status != 200) {
$this->log_err = $login->response_details;
return false;
}
$this->sid = $login->response->session_id;
return $this->logged = true;
}
public function download($link, $dir = false, $raw = false) {
if ($this->logged == null)
return false;
$res = json_decode($this->get("https://rapidgator.net/api/file/download?sid=" . $this->sid . "&url=" . $link));
if ($raw)
return $res;
if ($res->response_status != 200) {
$this->down_err = $res->response_details;
return false;
}
if ($dir == false)
$dir = "rapidgator_download";
if (!is_dir($dir))
mkdir($dir, 0776, true);
$info = $this->link_info($link);
$name = "file_unkn";
if ($info->response_status == 200)
$name = $info->response->filename;
$file = $this->get($res->response->url);
if ($file)
return file_put_contents($dir . DIRECTORY_SEPARATOR . $name, $file);
return false;
}
public function upload_file($file, $rapigator_foder = '') {
if ($this->logged == null)
return false;
if (!file_exists($file))
return false;
$folder = '';
if ($rapigator_foder != '')
$folder = '&' . $rapigator_foder;
$res = json_decode($this->get("https://rapidgator.net/api/file/dupload?sid=" . $this->sid . "&hash=" . md5_file($file) . "&size=" . filesize($file) . "&name=" . basename($file) . $folder));
if ($res->response_status != 200) {
$this->upl_err = $res->response_details;
return false;
}
if (!isset($res->response->url))
return $this->link = $res->response->link;
$url = $res->response->url;
$uuid = $this->upl_id = substr($url, strpos($url, "uuid=") + 5, 1 + strlen($url) - strpos($url, "&"));
$up_nw = json_decode($this->post_upload($res->response->url, realpath($file)));
return $this->link = $this->upload_info_file($uuid, true)->response->link;
}
public function upload_link($link, $raw = false) {
if ($this->logged == null)
return false;
$res = json_decode($this->get("https://rapidgator.net/api/file/upload?sid=" . $this->sid . "&url=" . $link));
if ($raw)
return $res;
if ($res->response_status != 200) {
$this->upl_err = $res->response_details;
return false;
}
$upl_id = $this->upl_id = $res->response->upload_job_id;
return $this->upload_info_link($upl_id, true)->response->status;
}
public function upload_info_link($upload_job_id, $raw = false) {
if ($this->logged == null)
return false;
$res = json_decode($this->get("https://rapidgator.net/api/file/upload_info?sid=" . $this->sid . "&upload_job_id=" . $upload_job_id));
if ($raw)
return $res;
if ($res->response_status != 200) {
$this->upl_err = $res->response_details;
return false;
}
return $res->response->status;
}
public function upload_info_file($uuid, $raw = false) {
if ($this->logged == null)
return false;
$res = json_decode($this->get("https://rapidgator.net/api/file/dupload_info?sid=" . $this->sid . "&uuid=" . $uuid));
if ($raw)
return $res;
if ($res->response_status != 200) {
$this->upl_err = $res->response_details;
return false;
}
return $res->response->status;
}
public function user_info($sid = false) {
if ($sid != false)
return json_decode($this->get("https://rapidgator.net/api/user/info?sid=" . $sid));
if ($this->logged == null)
return false;
return json_decode($this->get("https://rapidgator.net/api/user/info?sid=" . $this->sid));
}
public function link_info($link) {
if ($this->logged == null)
return false;
return json_decode($this->get("https://rapidgator.net/api/file/info?sid=" . $this->sid . "&url=" . $link));
}
private function post($url, $data) {
$query = http_build_query($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$res = curl_exec($ch);
if ($res === false)
$this->curl_err = curl_error($ch);
curl_close($ch);
return $res;
}
private function post_upload($url, $file) {
$ch = curl_init();
$data = array(
'file' => curl_file_create($file, mime_content_type($file), basename($file))
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data"
));
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
if ($res === false)
$this->curl_err = curl_error($ch);
curl_close($ch);
return $res;
}
private function get($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
if ($res === false)
$this->curl_err = curl_error($ch);
curl_close($ch);
return $res;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment