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 https://developers.box.com/docs/#files-upload-a-file. The example request provided at the above link 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
What I have written is:
<?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' => '@' . $file_up,
'parent' => array('id' => $folder_id)
);
$params_json = json_encode($params);
//execute CURL
$fp = fopen($file_out, "w"); //output file
$ch = curl_init();
$options = array(
CURLOPT_URL => $curl_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array("Authorization: Bearer " . $accessToken),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params_json,
CURLOPT_VERBOSE => true,
CURLOPT_FILE => $fp,
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
print_r (curl_getinfo($ch));
echo curl_error($ch);
curl_close($ch);
fclose($fp);
?>
The result is:
* 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
> 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
< HTTP/1.1 405 Method Not Allowed
< Allow: GET, OPTIONS, HEAD
< Content-Type: text/html;charset=UTF-8
< Content-Length: 0
< Date: Thu, 29 Jan 2015 18:07:49 GMT
< Age: 0
< Connection: keep-alive
< Server: ATS
<
* Connection #0 to host upload.box.com left intact
Array
(
[url] => https://upload.box.com/api/2.0/files/content
[content_type] => text/html;charset=UTF-8
[http_code] => 405
[header_size] => 202
[request_size] => 255
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 5.748647
[namelookup_time] => 5.007004
[connect_time] => 5.155813
[pretransfer_time] => 5.544732
[size_upload] => 55
[size_download] => 0
[speed_download] => 0
[speed_upload] => 9
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 5.748583
[redirect_time] => 0
[certinfo] => Array
(
)
)
* Closing connection #0
If I add echo $params_json;
I get:
{"name":"@\\home\\dianna\\box\\uploadfile.txt","parent":{"id":"2934811551"}}
If instead of the jkson encoded string at CURLOPT_POSTFIELDS => $params_json,
I pass the array with CURLOPT_POSTFIELDS => $params,
I get failed creating formpost data
. If at this point I edit the $file_up var to store the relative url for the file to be uploaded ($file_up = "uploadfile.txt";
this file and my php file are in the same folder), the output of my php file becomes:
Content-Length: 311
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------650eb8029292
* Done waiting for 100-continue
< HTTP/1.1 100 Continue
< Date: Thu, 29 Jan 2015 18:23:33 GMT
< Server: ATS
< HTTP/1.1 400 Bad Request
< server: ATS
< Date: Thu, 29 Jan 2015 18:23:34 GMT
< Cache-Control: no-cache, no-store
< Content-Type: application/json
< Content-Length: 277
< Age: 2
< Connection: keep-alive
<
* Connection #0 to host upload.box.com left intact
Array
(
[url] => https://upload.box.com/api/2.0/files/content
[content_type] => application/json
[http_code] => 400
[header_size] => 273
[request_size] => 260
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 7.698447
[namelookup_time] => 5.006963
[connect_time] => 5.154683
[pretransfer_time] => 5.540065
[size_upload] => 311
[size_download] => 277
[speed_download] => 35
[speed_upload] => 40
[download_content_length] => 277
[upload_content_length] => 311
[starttransfer_time] => 6.541224
[redirect_time] => 0
[certinfo] => Array
(
)
)
* Closing connection #0
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)
Any help will be greatly appreciated!