使用Restler v3上传文件

时间:2013-03-31 19:31:45

标签: php api rest restler

就在最近,对Restler v3(source)添加了对多部分/表单数据上传的支持,但我无法让它工作。在我的index.php文件中,我添加了:

$r->setSupportedFormats('JsonFormat', 'UploadFormat');

当我发布.txt文件时,我收到以下错误(这是预期的,因为默认的'允许'格式是'image / jpeg','image / png':

"error": {
    "code": 403,
    "message": "Forbidden: File type (text/plain) is not supported."
}

但是当我发布一个.jpg文件时,我收到了以下错误:

"error": {
    "code": 404,
    "message": "Not Found"
}

我错过了什么?这是我的功能:

function upload() {
    if (empty($request_data)) {
        throw new RestException(412, "requestData is null");
    }
    return array('upload_status'=>'image uploaded successfully');
}

1 个答案:

答案 0 :(得分:0)

我明白了!我需要一个post()功能!对于遇到我遇到的同一问题的任何人来说,这是我使用Restler 3上传文件的解决方案:

<强>的index.php

<?php
    require_once '../vendor/restler.php';
    use Luracast\Restler\Restler;

    $r = new Restler();    
    $r->addAPIClass('Upload');  
    $r->setSupportedFormats('JsonFormat', 'UploadFormat');

    $r->handle();

<强> upload.php的

<?php
    class Upload {
        function get(){
           if (empty($request_data)) {
              throw new RestException(412, "requestData is null");
           }
        }

        function post($request_data=NULL) {
           return array('upload_status'=>'image uploaded successfully!');
        }
    }