使用Box.com PHP API上传文件

时间:2014-12-02 14:30:49

标签: php curl box-api boxapiv2

我正在尝试使用Box API将文件上传到box.com。根据文档,卷曲请求必须如下所示:

curl https://upload.box.com/api/2.0/files/content \
  -H "Authorization: Bearer ACCESS_TOKEN" -X POST \
  -F attributes='{"name":nameOftheFile, "parent":{"id":parentId}}' \
  -F file=@file

这就是我的所作所为:

$token = "......";
$url = https://upload.box.com/api/2.0/files/content;
$file_upload;

foreach ($_FILES['file']['name'] as $position => $file) { 
    $file_upload = $_FILES['file']['tmp_name'][$position];
}
$json  = json_encode(array('name' => $file ,array('parent' => array('id' => 0))));
$attrs = array('attributes' => $json,'file'=>'@'.$file_upload);

$this->post($url,($attrs));

// Post function
function post($url,$fields){
    try {       
        $ch = curl_init();          
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Bearer '.$this->token
        ));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);          
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        self::$response = curl_exec($ch);
        curl_close($ch);

    } catch (Exception $e) {
        self::$response = $e->getMessage();
    }       

    return  self::$response;
}

但它不起作用。卷曲部分有什么不对吗?

3 个答案:

答案 0 :(得分:3)

使用CurlFile代替' @ path'解决了这个问题!

答案 1 :(得分:3)

<?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>

http://liljosh.com/uploading-files-to-box-content-api-v2/

答案 2 :(得分:-1)

$attributes = array('name'=>time().'.jpg','parent'=>array('id'=>$parent_id));

$params = array('attributes' => json_encode($attributes), 'file' => "@".realpath($filename)); $headers = array("Authorization: Bearer ".$accesstoken);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

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);

$data = curl_exec($ch);

curl_close($ch);