doo58088 2014-07-02 23:19
浏览 55
已采纳

如何使用PHP将图像发送到deviantsart rest API

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!

  • 写回答

1条回答 默认 最新

  • duanbinren8906 2014-07-03 00:18
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?