如何使用guzzlehttp

时间:2018-06-07 15:03:52

标签: php lumen guzzle box

我正在尝试通过其API将文件上传到Box驱动器。我将这个文件从邮递员上传到我自己的个人流明API上的端点,然后我打算将文件发送到Box API。但是,我一直从Box API获得500服务器错误响应。这是邮递员的要求。

enter image description here

以下是从邮递员那里获取数据的控制器功能:

public function addFile(Request $request) {
    $file = $request->file('file');
    $fileName = $request->get("name");

    if ($file->getSize() > 1000000) {
        throw new BadRequestException(
            "Your file size is too big. Should be less that 1 mb."
        );
    }

    $boxApi = new BoxApi();
    $fileUploadResponse = $boxApi->uploadFile($file, $fileName);
    if ($fileUploadResponse->getStatusCode() == 201) {
        return $this->respond("File has been uploaded successfully", 201);
    }
}

用于上传的BoxApi类方法是:

public function uploadFile(UploadedFile $file, $fileName)
{
    $endpoint = "/files/content";
    $response = $this->client->request(
        "POST",
        $this->boxApiUploadUrl . $endpoint,
        [
            "headers" => [
                "Authorization" => "Bearer AUTH_TOKEN",
                "Content-Type" => "multipart/form-data",
                "enctype" => "multipart/form-data"
            ],
            "multipart" => [
                [
                    "name" => "file",
                    "contents" => $file,
                    "filename" => $fileName
                ],
                [
                    "name" => "attributes",
                    "contents" => json_encode([
                        "name" => $fileName,
                        "parent" => ['id' => '30']
                    ])
                ]
            ]
        ]
    );

    return $response;

从我对堆栈溢出的所有研究中,我还没有发现任何代码同时传递headers密钥和multipart密钥。我不确定这是否可能是问题,但如果我删除headers,则会引发UnAuthorizedException

我无法弄清楚我做错了什么,或者这可能是API本身的问题。任何帮助将非常感激。

0 个答案:

没有答案