File upload not working with PHP curl method (https://developer.box.com/v2.0/reference#upload-a-file
AnsweredHello Everyone,
I am trying to implement Box file upload in php curl method. But its not working, i am not getting any response from Box file Api. Following is my curl method code. Can anybody please look into this issue and share your idea to resolve this issue.
$url = 'https://upload.box.com/api/2.0/files/content';
$attributes = array('name' => $file->filename, 'parent' => array('id' => '***number removed for privacy***47'));
$params = array('attributes' => json_encode($attributes), 'file' => "@".drupal_realpath($file->uri));
$headers = array("Authorization: Bearer ".$res_data['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);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errNo = curl_errno($ch);
$errStr = curl_error($ch);
curl_close($ch);
Note: i am getting only the curl response code 400.
Thanks,
VijeeshCiber
-
Hello Everyone,
I have find out the solution for this issue. Following is my working code with PHP CURL.
$url = 'https://upload.box.com/api/2.0/files/content';
$json = json_encode(array(
'name' => $file->filename,
'parent' => array('id' => $folder)
));
$params = array(
'attributes' => $json,
'file'=>new CurlFile(drupal_realpath($file->uri),$file->filemime,$file->filename)
);
$headers = array("Authorization: Bearer ".$res_data['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);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errNo = curl_errno($ch);
$errStr = curl_error($ch);
curl_close($ch); -
Hi !
Welcome to the Box Community and thank you for your first post!
Did the solution provided by the original poster not work for you? If not, can you give us more details on the kinds of issues you're facing? This will help other community members understand how to help you best!
Thank you again for posting, and hope to hear back from you soon!
Emma
Box Community Management Intern
-
Hi, Thanks,
ya, it's working below I provide code.
$pathoffile=$_FILES['boxfile']['tmp_name'];
$filename=$_FILES['boxfile']['name'];
$handle = fopen($pathoffile, "r");
$data = fread($handle, filesize($pathoffile));
$tokens = array();
$tokens[] = "tokenkey";
$serverKey ="Bearer tokenkey";
$url = 'https://upload.box.com/api/2.0/files/content';
$attributes = array('name' => $filename, 'parent' => array('id' => '0'));
$params = array('attributes' => json_encode($attributes));
$headers = array();
$headers[] = 'Content-Type: multipart/form-data';
$headers[] = 'Authorization: key='.$serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file' => curl_file_create($pathoffile),
'attributes' => json_encode($attributes)
));
$response = curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Please sign in to leave a comment.
Comments
5 comments