title: Struggling with Box V2 API to upload a file via PHP
body: <p>After feeling comfortable testing the Box API via postman I am now struggling to run the curl requests from a php file. I can run some of them, but I cannot figure out how to convert the the file upload request described at <a href="https://developers.box.com/docs/#files-upload-a-file" rel="nofollow">https://developers.box.com/docs/#files-upload-a-file</a>.
  The example request provided at the above link is:</p>
  
  <pre><code>curl https://upload.box.com/api/2.0/files/content \
    -H "Authorization: Bearer ACCESS_TOKEN" -X POST \
    -F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
    -F file=@myfile.jpg
  </code></pre>
  
  <p>What I have written is:</p>
  
  <pre><code>&lt;?php
  $folder_id = "2934811551"; //fake
  $accessToken = "QxYtOeSUCdlu5PwMwxUJSD5BeMP9AaoZ"; //fake
  $file_out = "\home\box\upload.json";  //store the JSON response
  $file_up = "\home\box\uploadfile.txt"; //file to upload
  
  $curl_url = 'https://upload.box.com/api/2.0/files/content';
  
  $params = array('name' =&gt; '@' . $file_up,
                  'parent' =&gt; array('id' =&gt; $folder_id)
                 );
  $params_json = json_encode($params);
  
  
  //execute CURL 
  $fp = fopen($file_out, "w"); //output file
  
  $ch = curl_init();
  $options = array(
           CURLOPT_URL            =&gt; $curl_url,           
           CURLOPT_RETURNTRANSFER =&gt; true,
           CURLOPT_HTTPHEADER     =&gt; array("Authorization: Bearer " . $accessToken),          
           CURLOPT_POST           =&gt; true,
           CURLOPT_POSTFIELDS     =&gt; $params_json,
           CURLOPT_VERBOSE        =&gt; true,
           CURLOPT_FILE           =&gt; $fp,     
          );
  curl_setopt_array($ch, $options);
  $result = curl_exec($ch);
  
  print_r (curl_getinfo($ch));
  echo curl_error($ch);
  
  curl_close($ch);
  
  fclose($fp);
  
  ?&gt;
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>* About to connect() to upload.box.com port 443 (#0)
  *   Trying 74.112.185.182... * connected
  * Connected to upload.box.com (74.112.185.182) port 443 (#0)
  * Initializing NSS with certpath: sql:/etc/pki/nssdb
  *   CAfile: /etc/pki/tls/certs/ca-bundle.crt
    CApath: none
  * SSL connection using TLS_RSA_WITH_AES_256_CBC_SHA
  * Server certificate:
  *   subject: CN=*.box.com,O="Box, Inc.",L=Los Altos,ST=California,C=US
  *   start date: Nov 13 19:22:20 2014 GMT
  *   expire date: Oct 23 02:42:03 2017 GMT
  *   common name: *.box.com
  *   issuer: CN=GeoTrust SSL CA - G4,O=GeoTrust Inc.,C=US
  &gt; POST /api/2.0/files/content HTTP/1.1
  Host: upload.box.com
  Accept: */*
  Authorization: Bearer QxYtOeSUCdlu5PwMwxUJSD5BeMP9AaoZ
  Content-Length: 55
  Content-Type: application/x-www-form-urlencoded
  
  &lt; HTTP/1.1 405 Method Not Allowed
  &lt; Allow: GET, OPTIONS, HEAD
  &lt; Content-Type: text/html;charset=UTF-8
  &lt; Content-Length: 0
  &lt; Date: Thu, 29 Jan 2015 18:07:49 GMT
  &lt; Age: 0
  &lt; Connection: keep-alive
  &lt; Server: ATS
  &lt; 
  * Connection #0 to host upload.box.com left intact
  Array
  (
      [url] =&gt; https://upload.box.com/api/2.0/files/content
      [content_type] =&gt; text/html;charset=UTF-8
      [http_code] =&gt; 405
      [header_size] =&gt; 202
      [request_size] =&gt; 255
      [filetime] =&gt; -1
      [ssl_verify_result] =&gt; 0
      [redirect_count] =&gt; 0
      [total_time] =&gt; 5.748647
      [namelookup_time] =&gt; 5.007004
      [connect_time] =&gt; 5.155813
      [pretransfer_time] =&gt; 5.544732
      [size_upload] =&gt; 55
      [size_download] =&gt; 0
      [speed_download] =&gt; 0
      [speed_upload] =&gt; 9
      [download_content_length] =&gt; 0
      [upload_content_length] =&gt; 0
      [starttransfer_time] =&gt; 5.748583
      [redirect_time] =&gt; 0
      [certinfo] =&gt; Array
          (
          )
  
  )
  * Closing connection #0
  </code></pre>
  
  <p>If I add <code>echo $params_json;</code> I get: </p>
  
  <pre><code>{"name":"@\\home\\dianna\\box\\uploadfile.txt","parent":{"id":"2934811551"}}
  </code></pre>
  
  <p>If instead of the jkson encoded string at <code>CURLOPT_POSTFIELDS =&gt; $params_json,</code> I pass the array with <code>CURLOPT_POSTFIELDS =&gt; $params,</code> I get <code>failed creating formpost data</code>. If at this point I edit the $file_up var to store the relative url for the file to be uploaded (<code>$file_up = "uploadfile.txt";</code> this file and my php file are in the same folder), the output of my php file becomes:</p>
  
  <pre><code>Content-Length: 311
  Expect: 100-continue
  Content-Type: multipart/form-data; boundary=----------------------------650eb8029292
  
  * Done waiting for 100-continue
  &lt; HTTP/1.1 100 Continue
  &lt; Date: Thu, 29 Jan 2015 18:23:33 GMT
  &lt; Server: ATS
  &lt; HTTP/1.1 400 Bad Request
  &lt; server: ATS
  &lt; Date: Thu, 29 Jan 2015 18:23:34 GMT
  &lt; Cache-Control: no-cache, no-store
  &lt; Content-Type: application/json
  &lt; Content-Length: 277
  &lt; Age: 2
  &lt; Connection: keep-alive
  &lt; 
  * Connection #0 to host upload.box.com left intact
  Array
  (
      [url] =&gt; https://upload.box.com/api/2.0/files/content
      [content_type] =&gt; application/json
      [http_code] =&gt; 400
      [header_size] =&gt; 273
      [request_size] =&gt; 260
      [filetime] =&gt; -1
      [ssl_verify_result] =&gt; 0
      [redirect_count] =&gt; 0
      [total_time] =&gt; 7.698447
      [namelookup_time] =&gt; 5.006963
      [connect_time] =&gt; 5.154683
      [pretransfer_time] =&gt; 5.540065
      [size_upload] =&gt; 311
      [size_download] =&gt; 277
      [speed_download] =&gt; 35
      [speed_upload] =&gt; 40
      [download_content_length] =&gt; 277
      [upload_content_length] =&gt; 311
      [starttransfer_time] =&gt; 6.541224
      [redirect_time] =&gt; 0
      [certinfo] =&gt; Array
          (
          )
  
  )
  * Closing connection #0
  </code></pre>
  
  <p>The content-Type now is set as multipart/form-data, but the response is still 400. 
  I have read all the answers on this topic here, and the PHP page manual page on curl, but this is as far as i have gotten... (btw, if it helps I am using PHP 5.3.3)</p>
  
  <p>Any help will be greatly appreciated!</p>