将文件上传到Box.com时出错

时间:2013-08-08 07:42:55

标签: php box-api

我正在编写一个PHP应用程序,它必须将pdf文件上传到我的企业Box.com帐户。

我收到400错误(请求中的输入参数无效),但我认为一切都是正确的。

function upload( $doc ) {

    if ( ! $this->access() )
        $this->getAccess();

    if ( ! is_readable($doc) )
        return false;

    $this->curl->clear();
    // $this->curl->setUpload( true );
    $this->curl->setMethod( 'POST' );
    $this->curl->setHeaders( $this->_header() );
    $this->curl->setValues( array(
        'parent_id' => 0,
        'filename'  => '@'.$doc
    ));
    $this->curl->exec( 'https://upload.box.com/api/2.0/files/content' );

    echo '<pre>'; print_r($this->curl->getHeaders()); echo '</pre>';
    echo '<pre>'; print_r($this->curl->getValues()); echo '</pre>';

    $response = $this->curl->getResult(true);

    echo "<pre>"; print_r($response); echo "</pre>"; exit;

}

结果:

POST /api/2.0/files/content HTTP/1.1
User-Agent: User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0) Gecko/20100101 Firefox/22.0
Host: upload.box.com
Accept: */*
Authorization: Bearer .....
Content-Length: 89
Content-Type: application/x-www-form-urlencoded

Array
(
    [parent_id] => 0
    [filename] => @/Applications/XAMPP/xamppfiles/htdocs/MyFile.pdf
)

stdClass Object
(
    [type] => error
    [status] => 400
    [code] => invalid_request_parameters
    [help_url] => http://developers.box.com/docs/#errors
    [message] => Invalid input parameters in request
    [request_id] => ......
)

当我取消注释用于制作cURL上传文件的行时,方法从POST更改为PUT,然后我收到错误405 Method Not Allowed。

根据此处的API文档http://developers.box.com/docs/#files-upload-a-file我正在正确发送所有内容。

1 个答案:

答案 0 :(得分:0)

为v2编写PHP SDK

只需包含api类并启动课程:

<?php
    include('library/BoxAPI.class.php');

    $client_id = 'CLIENT ID';
    $client_secret = 'CLIENT SECRET';
    $redirect_uri = 'REDIRECT URL';
    $box = new Box_API($client_id, $client_secret, $redirect_uri);

    if(!$box->load_token()){
        if(isset($_GET['code'])){
            $token = $box->get_token($_GET['code'], true);
            if($box->write_token($token, 'file')){
                $box->load_token();
            }
        } else {
            $box->get_code();
        }
    }
    // Upload file
    $box->put_file('RELATIVE FILE URL', '0'));
?>

看看here 下载:BoxPHPAPI