0

我已经设置了一个 HTML 表单来选择一个文件并将其提交给将上传它的 PHP 脚本。我不能使用move_uploaded_files(),因为 Box 的 API 要求我为Authorization: access_token. 我所做的是使用 cURL 库设置我自己的 POST 方法。

我遇到的问题是正确设置文件名,因为它需要文件的完整路径。我无法从 HTML 表单中获取文件的完整路径,并且无法使用$_FILES['filename']['tmp_name']上传我不想要的 .tmp 文件。有谁知道这个问题的解决方案?非常感谢!

我的代码:

public function upload_file($file) {
        $url = 'https://api.box.com/2.0/files/content';
        $params = [
            'filename' => '@'.$file['tmp_name'],
            'folder_id' => '0'
        ];
        $header = "Authorization: Bearer ".$this->access_token;
        $data = $this->post($url, $params, $header);
    }

public function post($url, $params, $header='') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        if(!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
        }
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }
4

2 回答 2

0

我同意 Vishal 在第一点中的建议。

我已经为 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'));
?>

看看这里 下载:BoxPHPAPI

于 2013-09-10T13:17:30.723 回答
0

建议您执行以下操作之一:

  1. 使用 move_uploaded_files 将文件上传到某个目录并使用该位置将文件发送到使用 curl 的框。上传成功后,您可以从此目录中删除文件。
  2. 您可以使用 cors http://developers.blog.box.com/2013/05/13/uploading-files-with-cors/在客户端上传文件,而不是通过 PHP 上传文件

我想到的另一个问题是你如何保持你的 access_token 刷新?

  • 维沙尔
于 2013-05-14T12:51:28.967 回答