Skip to content

Instantly share code, notes, and snippets.

@morningtoast
Last active August 29, 2015 13:55
Show Gist options
  • Save morningtoast/8721307 to your computer and use it in GitHub Desktop.
Save morningtoast/8721307 to your computer and use it in GitHub Desktop.
imageshack api curl
<pre>
<?
class ImageShack {
function auth($username, $password) {
$this->debug("Logging in");
$response = $this->doCurl("post", "https://api.imageshack.us/v1/user/login", array(
"user" => $username,
"password" => $password
));
$this->debug($response);
return($response);
}
function __construct($apikey, $username=false, $password=false, $cookie=false) {
$response = $this->auth($username, $password);
echo "foo";
$this->settings = (object) array(
"apikey" => $apikey,
"cookie" => $cookie,
"token" => $response["result"]["auth_token"],
"debug" => true
);
$this->debug($this->response);
}
function debug($m) {
if ($this->settings->debug) {
if (is_array($m)) { echo "<pre>".print_r($m)."</pre>"; } else { echo "<pre>".$m."</pre>"; }
}
}
function doCurl($method, $url, $params=array()) {
$data = array();
foreach($params as $key => $value) { $data[] = $key.'='.$value; }
$data = implode("&", $data);
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case "post":
curl_setopt($ch,CURLOPT_POST, count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
break;
case "delete":
curl_setopt($ch,CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
break;
}
//execute post
$result = curl_exec($ch);
$json = json_decode($result, true);
//close connection
curl_close($ch);
return($json);
}
function transloadImage($imageUrl, $resize=false) {
$this->debug("Transloading");
//$this->auth($this->settings->username, $this->settings->password);
$params = array(
"api_key" => $this->settings->apikey,
"url" => urlencode($imageUrl),
"format" => "json",
"auth_token" => $this->settings->token
);
if ($resize) {
$params["optimage"] = 1;
$params["optsize"] = $resize;
}
$response = $this->doCurl("post", "https://api.imageshack.us/v1/images", $params);
$this->debug($response);
return($response);
}
function deleteImage($imgsUrl) {
//imageshack.us/a/img547/7087/ss0w.jpg
$this->debug("Deleting image");
$url = explode("/", $imgsUrl);
$server = str_replace("img", "", $url[4]);
$filename = end($url);
$response = $this->doCurl("delete", "https://api.imageshack.us/v1/images/".$server."/".$filename, array(
"api_key" => $this->settings->apikey,
"auth_token" => $this->settings->token
));
$this->debug($response);
return($response);
}
}
$i = new ImageShack("xxx", "xxx","xxx");
//print_r($i->auth());
//$i->transloadImage("http://www.morningtoast.com/wordpress/wp-content/uploads/2013/11/mt-diehard-top.jpg", "200x200");
$i->deleteImage("http://imageshack.com/a/img837/5813/ag5k.jpg");
//print_r($r);
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment