3

I'm trying to upload a file to box using curl, I can create and view folders but not upload a file.

According to the documentation the curl request is:

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

I'm using this method:

public function put_file($filename, $name, $parent_id) {
$url = $this->upload_url . '/files/content';

$attributes = array('name' => $name, 'parent' => array('id' => $parent_id));
$params = array('attributes' => json_encode($attributes), 'file' => "@".realpath($filename));
$headers = array("Authorization: Bearer ".$this->access_token); 

$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);
$response = curl_exec($ch); 
curl_close($ch); 

return json_decode($response, true);
} 

The response I get back is:

Array
(
[url] => https://upload.box.com/api/2.0/files/content
[content_type] => text/html;charset=UTF-8
[http_code] => 400
[header_size] => 243
[request_size] => 228
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.616644
[namelookup_time] => 0.004139
[connect_time] => 0.151089
[pretransfer_time] => 0.465782
[size_upload] => 331
[size_download] => 0
[speed_download] => 0
[speed_upload] => 204
[download_content_length] => 0
[upload_content_length] => 331
[starttransfer_time] => 1.466139
[redirect_time] => 0
[redirect_url] => 
[primary_ip] => 74.112.184.85
[certinfo] => Array
(
) 
[primary_port] => 443
[local_ip] => 213.230.222.4
[local_port] => 52643
)

I've looked at similar questions on SO but cannot see what/where I'm going wrong.

The library I'm using is BoxPHPAPI

Dave
  • 878
  • 8
  • 19
  • Can you get a log of the actual HTTP request that's being made? If you're on OS X or Linux, an easy way to do this is to run `nc -l localhost 8080` and then make the cURL request to `http://localhost:8080/api/2.0/files/content` instead of Box. – Greg Jul 23 '15 at 17:55
  • on Windows unfortunately, looked at the headers curl was sending but cannot determine what is wrong, tried doing it via shell_exec instead works great. I'll go with that option. – Dave Jul 24 '15 at 12:53
  • In case it helps, here's an example of what the entire HTTP request should look like - http://stackoverflow.com/a/27759602/108 – Greg Jul 25 '15 at 03:45

1 Answers1

0

Not ideal but got this working using an shell_exec instead:

$cmd = "curl https://upload.box.com/api/2.0/files/content \
-H \"Authorization: Bearer $this->access_token\" -X POST \
-F attributes='{\"name\":\"$filename\",\"parent\": {\"id\":\"$parent_id\"}}' \
-F file=@\"$filename\"";

shell_exec($cmd);
Dave
  • 878
  • 8
  • 19