5

I am trying to upload a file to box.com using Box API. According to the docs, the curl request has to look like this:

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

Here's what I did:

$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;
}

But it doesn't work. Is there anything wrong in curl part?

nanocv
  • 2,227
  • 2
  • 14
  • 27
Prakhar
  • 2,270
  • 19
  • 26
  • you'll need at least `'file'=>"@$file_upload"` and provide JSON input instead of PHP arrays, see: http://stackoverflow.com/questions/21905942/posting-raw-image-data-as-multipart-form-data-in-curl for the file parts – Hans Z. Dec 02 '14 at 14:58
  • @HansZandbelt i use json_encode($attrs), so that's converted to json – Prakhar Dec 02 '14 at 15:00
  • you're not uploading a file. you're just stuffing a filename into some json and sending that over. And if you're allowing multiple file uploads under the `['file']` name, then your code would only ever upload the LAST file. Plus, you're simply assuming the upload succeeded. – Marc B Dec 02 '14 at 15:05
  • @MarcB thats a test, so i always put only one file, so im not doing any "Multiple file upload", – Prakhar Dec 02 '14 at 15:09
  • so you think the json i crafted doesn't have any issue ? – Prakhar Dec 02 '14 at 15:10
  • then why the foreach loop? And yes, it has issues. curl has no idea what to do with json. curl will see the josn as a solid block of text and send it out as-is. curl expects an array, or a string. If you provide the array, curl will url-encode it (and trigger file uploads if any of the field names start with `@`). If it's a string, curl sends it out as-is. – Marc B Dec 02 '14 at 15:14
  • you need json_encode only on the value of `attributes` and provide the an array with 2 attributes: array('attributes' => $json, 'file' => "@file_upload") – Hans Z. Dec 02 '14 at 16:04
  • it works now, but it says the '@filename API' is depreciated now ? – Prakhar Dec 02 '14 at 16:45

3 Answers3

3

Using CurlFile instead of '@path' fixes the issue!

Prakhar
  • 2,270
  • 19
  • 26
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/

Josh LaMar
  • 271
  • 3
  • 7
-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);
Phate01
  • 2,499
  • 2
  • 30
  • 55
Jai
  • 151
  • 1
  • 7