简体   繁体   English

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

[英]Unable to upload file to box using Api in php

I'm unable to upload file on box using php. 我无法使用php上传文件。 I've tried all the contents which I've found on stackOverflow but never got success. 我已经尝试了所有在stackOverflow上找到的内容,但从未成功。 Even I've tried https://github.com/golchha21/BoxPHPAPI/blob/master/README.md Client but still got failure. 即使我已经尝试过https://github.com/golchha21/BoxPHPAPI/blob/master/README.md客户端,但仍然失败。 Can anyone help me how to upload file on box using php curl. 任何人都可以帮助我如何使用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? 请告诉我我做错了什么?

I'm using Box PHP Client for creation of folders, uploading of files and then sharing of uploaded files using this client. 我正在使用Box PHP Client创建文件夹,上传文件,然后使用此客户端共享上传的文件。 https://github.com/golchha21/BoxPHPAPI 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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM