【问题标题】:Uploading image file to Strapi CMS API with Guzzle使用 Guzzle 将图像文件上传到 Strapi CMS API
【发布时间】:2021-06-14 14:53:16
【问题描述】:

我正在尝试使用他们的 API 以及 Laravel 和 Guzzle 将图像上传到 Strapi CMS。每当我发出 POST 请求时,简单的数据,如名称、描述等,都会被完美地发布,只有图像没有。甚至没有任何需要采取行动的错误,只有“200 次成功”。

我做错了吗?

这是我尝试过的: 到目前为止我的处理程序:

$this->processStrapiProductCategory([
            'multipart' => [
                [
                    'name' => 'ImageCover',
                    'contents' => fopen('storage/app/' . $sliderImageInternalUrl, 'r' ),
                ],
            ],
            'json' => [
                'TextCover' => $sliderText,
                'Description' => $categoryDescription,
                'Name' => $categoryName,
                'Slug' => $categorySlug,
            ],
        ]);

然后大吃一惊:

private function processStrapiProductCategory($options): void
    {
        $client = new \GuzzleHttp\Client(['verify' => false]);

        try {
            $response = $client->request('POST', '192.168.1.109:1337/product-categories/', $options);
            dump('StatusCode of guzzle request: ' . $response->getStatusCode());
        } catch (\Exception $e) {
            dump('Guzzle error: ' . $e->getMessage());
        }
    }

【问题讨论】:

  • 你解决了吗?

标签: php laravel content-management-system guzzle strapi


【解决方案1】:

这是您在查看 multipart 的 guzzle 文档时会发现的内容

此选项不能与 body、form_params 或 json 一起使用

改成下面

[
    'multipart' => [
        [
                    'name' => 'ImageCover',
                    'contents' => fopen('storage/app/' . $sliderImageInternalUrl, 'r' ),
        ],
        [
            'name' => 'data',
            'contents' => json_encode(
                [
                   'TextCover' => $sliderText,
                   'Description' => $categoryDescription,
                   'Name' => $categoryName,
                   'Slug' => $categorySlug,
                ]
            ),
        ]
    ]
]

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多