Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file with Box.com PHP API

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?

like image 960
Prakhar Avatar asked Dec 02 '14 14:12

Prakhar


2 Answers

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

like image 116
Prakhar Avatar answered Sep 19 '22 13:09

Prakhar


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

like image 30
Josh LaMar Avatar answered Sep 22 '22 13:09

Josh LaMar