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语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader