dsfs587465 2017-07-20 11:41
浏览 34
已采纳

无法使用PHP中的Api将文件上传到框

I'm unable to upload file on box using php. I've tried all the contents which I've found on stackOverflow but never got success. Even I've tried https://github.com/golchha21/BoxPHPAPI/blob/master/README.md Client but still got failure. Can anyone help me how to upload file on box using php curl.

$access_token = 'xGjQY2XU0bmOEwVAdkqiZTsGuFyFuqzU';
$url = 'https://upload.box.com/api/2.0/files/content';
$headers = array("Authorization: Bearer $access_token"
        . "Content-Type:multipart/form-data");
$filename = 'file.jpg';
$name = 'file.jpg';
$parent_id = '0';
$post = array('filename' => "@" . realpath($filename), 'name' => $name, 
'parent_id' => $parent_id);


$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
var_dump($response);

Even I've used this request too...

$localFile = "file.jpg";
$fp = fopen($localFile, 'r');
$access_token = '00hsilvu9LrAsKQ8iDzXZAAieSLrjzX9';
$url = 'https://upload.box.com/api/2.0/files/content';
$curl = curl_init();

$cfile = new CURLFILE($localFile, 'jpg', 'Test-filename.jpg');
$data = array();                
//$data["TITLE"] = "$noteTitle";
//$data["BODY"] = "$noteBody";
//$data["LINK_SUBJECT_ID"] = "$orgID";
//$data["LINK_SUBJECT_TYPE"] = "Organisation";        
$data['filename'] = "file.jpg";
$data['parent_id'] = 0;

curl_setopt_array($curl, array(
  CURLOPT_UPLOAD => 1,
  CURLOPT_INFILE => $fp,
  CURLOPT_NOPROGRESS => false, 
  CURLOPT_BUFFERSIZE => 128,
  CURLOPT_INFILESIZE => filesize($localFile),
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $data,      
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer 00hsilvu9LrAsKQ8iDzXZAAieSLrjzX9",
    "Content-Type:multipart/form-data"
  ),
));
$response = curl_exec($curl);
$info = curl_getinfo($curl);
$err = curl_error($curl);

curl_close($curl);
var_dump($info);
$req_dump = print_r($response, true);
file_put_contents('box.txt', $req_dump, FILE_APPEND);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

And in response I always got an empty string. Please tell me what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • doujie3888 2017-08-17 13:05
    关注

    I'm using Box PHP Client for creation of folders, uploading of files and then sharing of uploaded files using this client. https://github.com/golchha21/BoxPHPAPI

    But this client's uploading file wasn't working... then I dig into it and I found something missing into this method.

    /* Uploads a file */    
    
        public function put_file($filename, $name ,$parent_id) {
                $url = $this->build_url('/files/content', array(), $this->upload_url);
                if(isset($name)){
                    $name = basename($filename);
                }
                $params = array('filename' => "@" . realpath($filename), 'name' => $name , 'parent_id' => $parent_id, 'access_token' => $this->access_token);
                return json_decode($this->post($url, $params), true);
            }
    

    Just put forward slash after @ sign. Like this, Then I'll start working

    /* Uploads a file */    
    
        public function put_file($filename, $name ,$parent_id) {
                $url = $this->build_url('/files/content', array(), $this->upload_url);
                if(isset($name)){
                    $name = basename($filename);
                }
                $params = array('filename' => "@/" . realpath($filename), 'name' => $name , 'parent_id' => $parent_id, 'access_token' => $this->access_token);
                return json_decode($this->post($url, $params), true);
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算