【问题标题】:BOX-API upload file formBOX-API上传文件形式
【发布时间】:2014-01-20 09:27:29
【问题描述】:

我正在尝试使用 php 和 Zend 框架在 BOX_API 中上传文件。但我错过了一些东西。这是我第一次使用这样的界面,我读过manual。但这对我来说很混乱。我的问题是两个:

-首先,为什么您必须将文件名而不是具有正确标题的整个文件传递给 post 调用以进行文件上传?表单中的文件上传不像通过 post 调用传递文件名;

-其次,我是否必须制作一个用于文件上传的表单,或者只是一个文本区域,用于写入要传递给 BOX-API 的文件名?

更新: 这是我上传表单的代码:

    $form = new Zend_Form;
    $form->setAction('/imball-reagens/public/upload')
    ->setMethod('post');
    $file = new Zend_Form_Element_File('file');
    $file->setLabel('Choose a file to upload:');
    $file->addValidator('alnum');
    $file->setRequired(true);
    $form->addElement($file);
    $access_token = new Zend_Form_Element_Hidden(array('name' => 'access_token', 'value' => $result->access_token));
    $form->addElement($access_token);
    $refresh_token = new Zend_Form_Element_Hidden(array('name' => 'refresh_token', 'value' => $result->refresh_token));
    $form->addElement($refresh_token);
    $form->addElement('submit', 'upload', array('label' => 'Upload File'));
    echo $form;

这是表单后面的 box API 的 POST cal:

    $access_token= $this->getRequest()->getParam('access_token');
    $client = new Zend_Http_Client('https://upload.box.com/api/2.0/files/content');
    $client->setMethod(Zend_Http_Client::POST);
    $client->setHeaders('Authorization: Bearer '.$access_token);
    $data = $_FILES["file"]["name"];
    $client->setParameterPost(array(
            'filename'  => '@'.$data,
            'parent_id' => '0'
    ));
    $response = $client->request()->getBody();
    $this->view->response= $response;
    $result = json_decode($response);

它抛出的错误如下:

 {"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"172518183652dcf2a16af73"}

【问题讨论】:

    标签: file-upload box-api


    【解决方案1】:

    在没有看到所有代码的情况下进行调试很棘手,但在您粘贴的部分中,您似乎正在将 $_FILES["file"]["name"] 传递给 API - 这仅包含用户上传的文件的原始名称 - 您需要将位置传递给服务器上的文件,该文件将数据发送到 Box API 客户端,以便它可以抓取它并将其发送到 Box 服务器 - 这应该存储在 $_FILES["file"]["tmp_name"] 中。

    我建议将代码更改为此并重试:

    $access_token= $this->getRequest()->getParam('access_token');
    $client = new Zend_Http_Client('https://upload.box.com/api/2.0/files/content');
    $client->setMethod(Zend_Http_Client::POST);
    $client->setHeaders('Authorization: Bearer '.$access_token);
    $data = $_FILES["file"]["tmp_name"];
    $client->setParameterPost(array(
        'parent_id' => '0'
    ));
    $client->setFileUpload($data, 'filename');
    $response = $client->request()->getBody();
    $this->view->response= $response;
    $result = json_decode($response);
    

    【讨论】:

    • 我已经尝试过了,但响应总是相同的错误。我想我也必须检查其他参数。还是谢谢你的回答!
    • 错误是否可能只是在 URL 行中?我应该写“http”还是“https”?
    • 奇怪,也许先用move_uploaded_file - uk1.php.net/move_uploaded_file 将文件保存在某处,然后将文件名和路径提供给Box API?我认为 Box API 需要 HTTPS。另外,我会检查您是如何获取访问令牌的,也许错误就在那里。
    • 我认为 Zend http 客户端的上传方式可能有点不同 - 试试这个要点gist.github.com/madebydavid/8541566
    • Box API 不需要 @ 符号 - @ 语法仅适用于 curl - 因此 curl 知道 @ 符号后面的任何内容都是文件名。对于 Zend 客户端,您有 setFileUpload 函数 - 所以您不需要 @ 符号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    相关资源
    最近更新 更多