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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测