title: Upload file with Box.com PHP API
body: <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here's what I did:</p>
  
  <pre><code>$token = "......";
  $url = https://upload.box.com/api/2.0/files/content;
  $file_upload;
  
  foreach ($_FILES['file']['name'] as $position =&gt; $file) { 
      $file_upload = $_FILES['file']['tmp_name'][$position];
  }
  $json  = json_encode(array('name' =&gt; $file ,array('parent' =&gt; array('id' =&gt; 0))));
  $attrs = array('attributes' =&gt; $json,'file'=&gt;'@'.$file_upload);
  
  $this-&gt;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-&gt;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-&gt;getMessage();
      }       
  
      return  self::$response;
  }
  </code></pre>
  
  <p>But it doesn't work. Is there anything wrong in curl part?</p>