6

在本页面:http://developers.box.com/docs/

使用 cURL 上传文件:

METHOD
POST /files/content
EXAMPLE REQUEST
curl https://api.box.com/2.0/files/content \
-H "Authorization: BoxAuth api_key=API_KEY&auth_token=AUTH_TOKEN" \
-F filename1=@FILE_NAME1 \
-F filename2=@FILE_NAME2 \
-F folder_id=FOLDER_ID

但是现在,我想使用 php 上传文件,我该怎么做呢?我的代码:

<?php     
$params = array();
$params['folder_id'] = '485272014';

$u_file = fopen("D:\code\php\bcs\test.data", "r");

$params['filename1'] = $u_file;

$params = json_encode($params);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/files/content");
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

curl_setopt($ch, CURLOPT_UPLOAD, true);



curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: BoxAuth api_key=API_KEY&auth_token=TOKEN"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

fclose($u_file);

?> 

它没有用,我使用以下命令运行脚本:php -f test.php

4

2 回答 2

6
  1. 我不认为 POST 表单数据可以接受使用创建的文件处理程序
    fopen("D:\code\php\bcs\test.data", "r");

    尝试使用 @ 而不是访问文件处理程序。顺便说一句,将 \ 更改为 / 这样你就不会不小心将某些字符作为转义字符:
    $u_file = "@D:/code/php/bcs/test.data";

  2. 你不应该json_encode是内容,如果你的文件内容不是文本(比如,图像/二进制文件)。

  3. 我认为这条线也给你带来了问题。使用此选项尝试了我的代码,向我抛出了一个奇怪的“441 Required length”错误。没有此选项,我的代码可以正常工作:
    curl_setopt($ch, CURLOPT_UPLOAD, true);

最后,这是我的工作代码:

<?php
public function upload_file()
{
   $url = 'https://api.box.com/2.0/files/content';

   $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_ENCODING, "UTF-8");

   //this is my method to construct the Authorisation header
   $header_details = array($this->default_authen_header());
   curl_setopt($ch, CURLOPT_HTTPHEADER, $header_details);

   $post_vars = array();
   $post_vars['filename'] = "@C:/tmp_touch.txt";
   $post_vars['folder_id'] = 0;

   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
   curl_setopt($ch, CURLOPT_URL, $url);

   $data = curl_exec($ch);
   curl_close($ch);
   return $data;
}
?>
于 2012-11-18T10:38:52.300 回答
6
<?php
    // ENTER YOUR DEVELOPER TOKEN
    $token = "ekdfokeEdfdfkosdkoqwekof93kofsdfkosodSqd";

    $url = "https://upload.box.com/api/2.0/files/content";
    if (isset($_POST['btnUpload'])) {
        $file_upload = $_FILES['file']['tmp_name'];
        $json = json_encode(array(
                                'name' => $_FILES['file']['name'], 
                                'parent' => array('id' => 0)
                            ));
        $fields = array(
                      'attributes' => $json,
                      'file'=>new CurlFile($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name'])
                  );

        try {
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer '.$token, 
                'Content-Type:multipart/form-data'
            ));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            $response = curl_exec($ch);
            curl_close($ch);
        } catch (Exception $e) {
            $response = $e->getMessage();
        }

        print_r($response);
    }

?>

<form method="post" name="frmUpload" enctype="multipart/form-data">
    <tr>
        <td>Upload</td>
        <td align="center">:</td>
        <td><input name="file" type="file" id="file"/></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td align="center">&nbsp;</td>
        <td><input name="btnUpload" type="submit" value="Upload" /></td>
    </tr>
</form>
于 2015-07-31T12:23:05.950 回答