forked from null-none/ImgurUploadPHP
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request null-none#1 from pkrumins/master
add error checking. create private/public interface. improve code and readme.
- Loading branch information
Showing
2 changed files
with
159 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
# ImgurUploadPHP | ||
Class for use imgur API. | ||
|
||
PHP class for quickly uploading images to Imgur. | ||
|
||
``` | ||
$imgur = new Imgur; | ||
$imgur->clientId = 'code'; | ||
$filename = 'images/browserling.png'; | ||
$handle = fopen($filename, "r"); | ||
$data = fread($handle, filesize($filename)); | ||
$params = array('image' => base64_encode($data)); | ||
$result = $imgur->uploadImage($params); | ||
print_r($result); | ||
$imgur = new ImgurUploader; | ||
$imgur->setClientId('clientID'); | ||
$result = $imgur->uploadImage('images/browserling.png'); | ||
if (!$result) { | ||
$error = $imgur->getError(); | ||
print "Error: $error"; | ||
} | ||
print_r($result); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,151 @@ | ||
<?php | ||
class Imgur { | ||
var $clientId; | ||
var $timeout = 30; | ||
|
||
function curlQuery($params, $url) { | ||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $url); | ||
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $this->clientId)); | ||
curl_setopt($curl, CURLOPT_POST, 1); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); | ||
$out = curl_exec($curl); | ||
curl_close ($curl); | ||
return json_decode($out,true); | ||
} | ||
|
||
function uploadImage($params) { | ||
$url = 'https://api.imgur.com/3/image.json'; | ||
return $this->curlQuery($params, $url); | ||
} | ||
} | ||
|
||
$imgur = new Imgur; | ||
$imgur->clientId = 'code'; | ||
$filename = 'images/browserling.png'; | ||
$handle = fopen($filename, "r"); | ||
$data = fread($handle, filesize($filename)); | ||
$params = array('image' => base64_encode($data)); | ||
$result = $imgur->uploadImage($params); | ||
print_r($result); | ||
|
||
class ImgurUploader { | ||
private $clientId; | ||
private $error; | ||
|
||
const TIMEOUT = 30; | ||
|
||
public function setClientId($clientId) { | ||
$this->clientId = $clientId; | ||
} | ||
|
||
public function getError () { | ||
return $this->error; | ||
} | ||
|
||
public function uploadImage($filename) { | ||
if (empty($this->clientId)) { | ||
$this->setError("No clientID set"); | ||
return FALSE; | ||
} | ||
|
||
if (!file_exists($filename)) { | ||
$this->setError("Filename '$filename' doesn't exist"); | ||
return FALSE; | ||
} | ||
|
||
if (!is_readable($filename)) { | ||
$this->setError("Filename '$filename' isn't readable"); | ||
return FALSE; | ||
} | ||
|
||
$handle = fopen($filename, "rb"); | ||
if (!$handle) { | ||
$this->setError("Failed opening '$filename' for reading"); | ||
return FALSE; | ||
} | ||
|
||
$data = file_get_contents($filename); | ||
if (!$data) { | ||
fclose($handle); | ||
$this->setError("Failed reading contents of '$filename'"); | ||
return FALSE; | ||
} | ||
fclose($handle); | ||
|
||
return $this->curlUpload([ | ||
'image' => base64_encode($data) | ||
]); | ||
} | ||
|
||
public function uploadImageBase64($imageBase64) { | ||
if (empty($this->clientId)) { | ||
$this->setError("No clientID set"); | ||
return FALSE; | ||
} | ||
|
||
return $this->curlUpload([ | ||
'image' => $imageBase64 | ||
]); | ||
} | ||
|
||
private function curlUpload($params) { | ||
$url = 'https://api.imgur.com/3/image.json'; | ||
return $this->curlQuery($params, $url); | ||
} | ||
|
||
private function curlQuery($params, $url) { | ||
$curl = curl_init(); | ||
if (!$curl) { | ||
$errstr = $this->getCurlStrError($curl); | ||
$this->setError("Failed initializing curl: $errstr"); | ||
return FALSE; | ||
} | ||
|
||
$ret = curl_setopt($curl, CURLOPT_URL, $url); | ||
if (!$ret) { | ||
$errstr = $this->getCurlStrError($curl); | ||
$this->setError("Failed setting CURLOPT_URL: $errstr"); | ||
return FALSE; | ||
} | ||
|
||
$ret = curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT); | ||
if (!$ret) { | ||
$errstr = $this->getCurlStrError($curl); | ||
$this->setError("Failed setting CURLOPT_TIMEOUT: $errstr"); | ||
return FALSE; | ||
} | ||
|
||
$ret = curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Authorization: Client-ID ' . $this->clientId ]); | ||
if (!$ret) { | ||
$errstr = $this->getCurlStrError($curl); | ||
$this->setError("Failed setting CURLOPT_HTTPHEADER: $errstr"); | ||
return FALSE; | ||
} | ||
|
||
$ret = curl_setopt($curl, CURLOPT_POST, 1); | ||
if (!$ret) { | ||
$errstr = $this->getCurlStrError($curl); | ||
$this->setError("Failed setting CURLOPT_POST: $errstr"); | ||
return FALSE; | ||
} | ||
|
||
$ret = curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
if (!$ret) { | ||
$errstr = $this->getCurlStrError($curl); | ||
$this->setError("Failed setting CURLOPT_RETURNTRANSFER: $errstr"); | ||
return FALSE; | ||
} | ||
|
||
$ret = curl_setopt($curl, CURLOPT_POSTFIELDS, $params); | ||
if (!$ret) { | ||
$errstr = $this->getCurlStrError($curl); | ||
$this->setError("Failed setting CURLOPT_POSTFIELDS: $errstr"); | ||
return FALSE; | ||
} | ||
|
||
$out = curl_exec($curl); | ||
if (!$out) { | ||
$errstr = $this->getCurlStrError($curl); | ||
$this->setError("Curl session failed: $errstr"); | ||
return FALSE; | ||
} | ||
|
||
curl_close($curl); | ||
return json_decode($out, true); | ||
} | ||
|
||
private function getCurlStrError($curl) { | ||
$errno = curl_errno($curl); | ||
$errstr = curl_strerror($errno); | ||
return $errstr; | ||
} | ||
|
||
private function setError($error) { | ||
$this->error = $error; | ||
} | ||
} | ||
|
||
$imgur = new ImgurUploader; | ||
$imgur->setClientId('clientID'); | ||
$result = $imgur->uploadImage('browserling.png'); | ||
if (!$result) { | ||
$error = $imgur->getError(); | ||
print "Error: $error"; | ||
} | ||
|
||
print_r($result); | ||
|
||
?> | ||
|