【问题标题】:How to send an image to deviantsart rest API with PHP如何使用 PHP 将图像发送到 deviantsart REST API
【发布时间】:2014-08-23 22:07:48
【问题描述】:

这里,其中一个响应 (#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 你的图片.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();
    }
}

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

【问题讨论】:

  • 结果是……?
  • 您应该检查表单中的$_FILES 以及$_POST,加上addPostFile 至少需要2 个参数,第一个是传出的$_FILES 关键元素,第二个是本地路径到文件中。
  • jcaron,没有结果。
  • Loz,如果我从 $_FILES 执行 ir 也是一样
  • HttpRequest 来自哪个库?也许值得先运行一个简单的 GET 操作,以检查您的凭据/连接是否正常。如果您需要一个简单的 REST 库,请考虑 PEST - 它在 GitHub 上。

标签: php rest post file-upload


【解决方案1】:

是的,它应该很简单,但是......很难实现简单;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>

【讨论】:

  • 嗨,谢谢我要去试试。然后我会评论,它必须像那样工作。
  • 抱歉我的无知,但在阅读您的回复之前,我认为在通过 POST 发送之前无需将图像上传到服务器......
猜你喜欢
  • 2020-09-03
  • 2019-04-06
  • 2017-12-04
  • 2022-01-25
  • 2017-11-09
  • 1970-01-01
  • 2020-05-26
  • 2017-08-16
  • 2019-08-07
相关资源
最近更新 更多