3

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!

Community
  • 1
  • 1
k1r0s
  • 359
  • 3
  • 14
  • And the result is...? – jcaron Jul 02 '14 at 23:21
  • You should be checking for `$_FILES` from the form as well as `$_POST`, plus `addPostFile` requires min 2 parameters, the first one is the outgoing $_FILES key element and second the local path to the file. – Lawrence Cherone Jul 02 '14 at 23:32
  • jcaron, there is no result. – k1r0s Jul 02 '14 at 23:36
  • Loz, its the same if i do ir from $_FILES – k1r0s Jul 02 '14 at 23:37
  • What library is `HttpRequest` from? Perhaps it is worth running a simple GET operation first, to check your credentials/connection are working okay. If you need a simple REST library, consider PEST - it's on GitHub. – halfer Jul 03 '14 at 00:08

1 Answers1

6

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Hi, thanks im going to try it. Then i will comment, it must work like that. – k1r0s Jul 03 '14 at 20:12
  • Sorry about my ignorance but before read your response i thought that there is no need to upload the image to the server before send through POST... – k1r0s Jul 03 '14 at 20:13