Skip to content

Instantly share code, notes, and snippets.

@charles-hollenbeck
Last active July 17, 2022 02:26
Show Gist options
  • Save charles-hollenbeck/6267660 to your computer and use it in GitHub Desktop.
Save charles-hollenbeck/6267660 to your computer and use it in GitHub Desktop.
Pastebin API in PHP; $pastebin = new Pastebin("DEVKEY"); $pastebin->login("USER", "PASS"); $pastebin->deletePaste("pasteid");
<?
class Pastebin{
private $token;
private $devkey;
private $loginURL = 'http://www.pastebin.com/api/api_login.php';
private $pasteURL = "http://www.pastebin.com/api/api_post.php";
function __construct($devkey){
$this ->devkey = $devkey;
}
function login($username, $password){
$api_user_name = urlencode($username);
$api_user_password = urlencode($password);
$data = 'api_dev_key='.$this->devkey.'&api_user_name='.$api_user_name.'&api_user_password='.$api_user_password;
$response = trim($this->page($this->loginURL, $data),"\n");
$check = $this->checkResponse($response);
if($check !== null)
return $check;
$this->token = $response;
return true;
}
function makePaste($code,$private,$name,$expire,$format,$anonymous=null){
$user = $this->token;
if($anonymous)
$user = null;
$content = urlencode($code);
$title = urlencode($name);
$data = 'api_option=paste&api_user_key='.$user.'&api_paste_private='.$private.'&api_paste_name='.$title.'&api_paste_expire_date='.$expire.'&api_paste_format='.$format.'&api_dev_key='.$this->devkey.'&api_paste_code='.$content;
$response = trim($this->page($this->pasteURL,$data),"\n");
$check = $this->checkResponse($response);
if($check !== null)
return $check;
return $response;
}
function deletePaste($pasteid){
if(empty($this->token))
return "Need to login first!";
$data = 'api_option=delete&api_user_key='.$api_user_key.'&api_dev_key='.$api_dev_key.'&api_paste_key='.$api_paste_key;
$response = $this->page($this->pasteURL,$data);
$check = $this->checkResponse($response);
if(!empty($check))
exit($check);
return true;
}
function listpastes($resultslimit, $json = false){
if(empty($this->token))
return "Need to login first!";
$data = 'api_option=list&api_user_key='.$this->token.'&api_dev_key='.$this->devkey.'&api_results_limit='.$resultslimit;
$response = trim($this->page($pasteURL, $data),"\n");
$arr = $this->parseXML2Array($response);
if($json)
return json_encode($arr);
return $arr;
}
function listTrending($json = null){
$postdata = "api_option=trends&api_dev_key=".$this->devkey;
$response = $this->page($this->pasteURL,$postdata);
$arr = $this->parseXML2Array($response);
if($json)
return json_encode($arr);
return $arr;
}
private function page($url,$data){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$r = curl_exec($ch);
return $r;
}
private function parseXML2Array($data){
$xml = simplexml_load_string("<?xml version='1.0'?><content>$data</content>");
$arr = array();
foreach($xml->children() as $child){
$data = array();
foreach($child->children() as $nextchild)
$data[$nextchild->getName()] = (string)$nextchild;
$arr[] = $data;
}
return $arr;
}
private function checkResponse($response){
if(substr($response,0,15) == "Bad API request")
return substr($response,17);
return null;
}
}
?>
@LurkRet
Copy link

LurkRet commented Jul 17, 2022

но тнянопеН

@LurkRet
Copy link

LurkRet commented Jul 17, 2022

дерБ

@LurkRet
Copy link

LurkRet commented Jul 17, 2022

Ноио

@LurkRet
Copy link

LurkRet commented Jul 17, 2022

Мирошщщщбпп

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment