dqlk31541 2015-06-20 01:00
浏览 45

Box api v2使用php上传文件

I have been working on box api. I could get everything to work but the file upload. Here is the code:

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

and pass all of that to a post function:

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

I get 400 error using this but if I add this line $body = http_build_query($body, '', '&'); before calling the post function I'll get a 405 error.

In both cases though the upload is not working. I made sure to pass the actual path of the file not the relative path in case anyone is wondering.

I also checked solutions like this: https://github.com/golchha21/BoxPHPAPI but that didn't work either.

Any idea on the issue and how to fix it?

  • 写回答

1条回答 默认 最新

  • doupeng8494 2015-07-15 20:32
    关注

    Have you attempted making this same request in the Chrome plugin POSTman? I am not sure about your post function but here is the command in curl

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

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题