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

原文 标签 php rest post file-upload

在这里,其中一个响应(#3)告诉:


http://deviantsart.com有一个易于使用的公共API,只需将图片HTTP POST到其域中,您将获得一个带有URL的JSON


这是网址:

Are there any image hosting services with a public API?

唯一的指示基本上是


“使用我们的公共REST API上传:POST http://deviantsart.com
yourimage.jpg

JSON结果:

{ "url" : "urltoimage" }"



很好,但是,如何使它可编程?

这是我的代码:

//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();
    }
}


最后编辑:请不要在意我的代码,尝试用自己的代码解决。我只想看看它是如何工作的。非常感谢!

最佳答案

是的,它应该很简单,但是...它的实现很难; p

这是一个例子。我没有使用或没有PECL HttpRequest类,所以我添加了cURL以防万一,如果您什么都没得到。您应该检查错误日志并启用错误报告。

<?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>

相关文章:

php - 从字符串中提取URL

java - Java:JPA / JSON / REST往返工作流程

jquery - jQuery ajax发布目录

php - 自动转换百分比输入值

php - 如何从一页将条目插入不同的表?

php - 詹金斯在部署时创建文件而不是符号链接

java - 我们可以在Java中有多个预定的线程池吗?

java - Grails项目中的ClassCastException

c# - 如何将图像和POST数据上载到Azure Mobile Services ApiController端点?

android - android posturl https