0
votes

Upload a New Version of a File method of box api in php not working

here is the code that I use to send request

$uploadfile =$_SESSION['uploadfile'];
            $fileid =$_SESSION['fileid'];
            $myuri='https://upload.box.com/api/2.0/files/'.$fileid.'/content';
            $curl2 = curl_init();

            curl_setopt($curl2, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$_SESSION['usertoken']));
            curl_setopt_array($curl2, array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_URL => $myuri,
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => array(
                filename => "@".$uploadfile
                )
            ));
            // Send the request & save response to $resp
            $data = curl_exec($curl2);
            $data = json_decode($data);

I get the following error response back from box:

object(stdClass)#1 (7) { ["type"]=> string(5) "error" ["status"]=> int(400) ["code"]=> string(11) "bad_request" ["context_info"]=> object(stdClass)#2 (1) { ["errors"]=> array(1) { [0]=> object(stdClass)#3 (3) { ["reason"]=> string(17) "missing_parameter" ["name"]=> string(6) "parent" ["message"]=> string(20) "'parent' is required" } } } ["help_url"]=> string(38) "http://developers.box.com/docs/#errors" ["message"]=> string(11) "Bad Request" ["request_id"]=> string(23) "1638286030525d1c87c6a1e" } 

according to their documentation i only need the file name of the file i am trying to upload and file Id of the file i am trying to overwrite it says nothing about requiring a parent what am I missing here? Is this a bug or is the documentation wrong?

the documentation can be found here http://developers.box.com/docs/#files-upload-a-new-version-of-a-file

1

1 Answers

0
votes

I figured this out the box documentation is shitty. Word to the wise if its listed as a parameter in the documentation it is required weather its listed as required or not. here is the working code

$fileid = $_REQUEST["fileid"];
        $uploadfile =$_SESSION['uploadfile'];
        $myuri="https://api.box.com/2.0/files/".$fileid;
        $curl = curl_init($myuri);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$_SESSION['usertoken']));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($curl);
        curl_close($curl);
        $data = json_decode($data);
        //var_dump($data);
        $etag =$data->etag;
        date_default_timezone_set("UTC");
        $timestamp = date(DATE_ATOM);
        $curl2 = curl_init();
        curl_setopt($curl2, CURLOPT_HTTPHEADER, array('If-Match: '.$etag,'Authorization: Bearer '.$_SESSION['usertoken']));
        curl_setopt_array($curl2, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => 'https://upload.box.com/api/2.0/files/'.$fileid.'/content',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => array(
                filename => "@".$uploadfile,
                content_modified_at =>$timestamp
                )
            ));

        $data2 = curl_exec($curl2);
        $data2 = json_decode($data2);