0

我一直在研究盒子 api。除了文件上传,我可以让一切正常工作。这是代码:

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

并将所有这些传递给 post 函数:

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, '', '&');在调用 post 函数之前添加这一行,我会得到一个 405 错误。

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

我还检查了这样的解决方案:https ://github.com/golchha21/BoxPHPAPI但这也不起作用。

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

4

1 回答 1

0

您是否尝试过在 Chrome 插件 POSTman 中发出同样的请求?我不确定您的 post 功能,但这是 curl 中的命令

curl https://upload.box.com/api/2.0/files/content -H "授权:Bearer ACCESS_TOKEN" -X POST -F attributes='{"name":"myFile.jpeg", "parent":{ "id":"0"}}' -F 文件=@FILE_PATH

于 2015-07-15T20:32:49.810 回答