2015-06-20 76 views
0

我一直在做box api。我可以让一切工作,但文件上传。下面是代码:Box api v2使用php上传文件

$path = 'https://upload.box.com/api/2.0/files/content'; 
$method = 'POST'; 
$headers = array('Authorization Bearer ' . $accessToken); 
$attributes = array('name'=>$file_name, 'parent'=>array('id'=>0)); 
$body = array('attributes'=>json_encode($attributes), 'file'=>'@'.realpath($filepath)); 
$result = $this->post($path, $method, $body, $headers); 

和所有的传递到后功能:

function post($url,$method, $fields, $headers){ 
    try { 
     $ch = curl_init(); 
     curl_setopt($ch,CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
     $response = curl_exec($ch); 
     $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

     if (false === $response) { 
      $errNo = curl_errno($ch); 
      $errStr = curl_error($ch); 
     } 
     curl_close($ch); 

    } catch (Exception $e) { 
     $response = $e->getMessage(); 
    } 

    return $responseCode; 
} 

我得到使用这个400错误,但如果我调用函数后前加入这一行$body = http_build_query($body, '', '&');我会得到一个405错误。

在这两种情况下,虽然上传不起作用。我一定要传递文件的实际路径,而不是相对路径,以防万一有人想知道。

我也检查过这样的解决方案:https://github.com/golchha21/BoxPHPAPI但这也没有效果。

关于这个问题的任何想法以及如何解决它?

回答

0

您是否试图在Chrome插件POSTMAN中提出相同请求?我不确定你的发布功能,但这里是curl命令

curl https://upload.box.com/api/2.0/files/content -H“授权:承载ACCESS_TOKEN”-X POST -F attributes ='{“name”:“myFile.jpeg”,“parent “:{”id“:”0“}}'-F file = @ FILE_PATH