Here, one of the responses (#3) tells:
http://deviantsart.com has a public and easy to use API just HTTP POST the image to their domain and you will get a JSON with the URL
Here is the URL:
Are there any image hosting services with a public API?
The only instructions are basically
"Upload using our public REST API: POST http://deviantsart.com yourimage.jpg
JSON-Result:
{ "url" : "urltoimage" }"
Nice, but, how can I make it programmable?
Here is my code:
//the file was uploaded by a simple html form
if ($_POST) { //submit
$tmp = array_merge($_POST);
$r = new HttpRequest('http://deviantsart.com', HttpRequest::METH_POST);
$r->addPostFile($tmp['img']); //tmp has post vars
echo $r->getUrl();
try {
echo $r->send()->getBody();
exit();
} catch (HttpException $ex) {
echo $ex;
exit();
}
}
LAST EDIT: please, dont care about my code, try to solve with your own. I just want see how it works. Many thanks!
Yes it should be simple but... it's the implementation of simple that's hard ;p
Here is an example. I don't use or have PECL HttpRequest
class, so I've added cURL just in case, if you're getting nothing. You should check your error logs and enable error reporting.
<?php
//check is POST
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//check image upload, your want to check for other things too like: is it an image?
if(isset($_FILES['img']['name'])){
//make filename for new file
$uploadfile = basename($_FILES['img']['name']);
//move the upload
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
/* HttpRequest - I dont use it, it should work but if like me,
its class not found, then this condition will revert to curl */
if(class_exists('HttpRequest')){
try {
$r = new HttpRequest('http://deviantsart.com', HttpRequest::METH_POST);
$r->addPostFile('file', $_SERVER['DOCUMENT_ROOT'].'/'.$uploadfile);
$resp = $r->send()->getBody();
} catch (HttpException $ex) {
$resp = $ex;
}
}
//simplez curl POST file
else{
$ch = curl_init('http://deviantsart.com');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file'=>'@'.$_SERVER['DOCUMENT_ROOT'].'/'.$uploadfile,
));
$resp = curl_exec($ch);
}
//decode the json response
$resp = json_decode($resp, true);
}
}
}
//output the image from deviantsart
if(isset($resp['url'])){
echo '<img src="'.$resp['url'].'"/>';
}
?>
<form enctype="multipart/form-data" action="" method="POST">
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="img" type="file" />
<input type="submit" value="Send File" />
</form>