无法在php

时间:2017-07-20 11:41:57

标签: php file curl upload box

我无法使用php上传文件。我已经尝试了我在stackOverflow上找到的所有内容,但从未取得过成功。即使我尝试了https://github.com/golchha21/BoxPHPAPI/blob/master/README.md客户端,但仍然失败了。谁能帮助我如何使用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);

即使我也使用过这个请求......

$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;
}

作为回应,我总是得到一个空字符串。 请告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

我使用Box PHP Client创建文件夹,上传文件,然后使用此客户端共享上传的文件。 https://github.com/golchha21/BoxPHPAPI

但是这个客户的上传文件还没有工作......然后我深入研究它,我发现这个方法缺少了一些东西。

/* 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);
        }

在@符号之后提出斜线。像这样,那我就开始工作了

/* 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);
        }