使用GuzzleHTTP

时间:2019-03-07 08:58:53

标签: laravel guzzle

我正在尝试通过UploadedFile将Laravel GuzzleHTTP实例发送到另一个应用程序。 post可以通过,但是多部分表单不能正确传递:

        $image_path = $data['post_field']->getPathname();
        $image_mime = $data['post_field']->getmimeType();
        $image_org  = $data['post_field']->getClientOriginalName();

        $response = $this->client->post($uri, [
            'headers'   => [
                'Content-Type' => 'application/json',
                'Authorization' =>  'Bearer ' . $bearerToken,
            ],
            'multipart' => [
                [
                    'name'     => 'post_field',
                    'filename' => $image_org,
                    'Mime-Type'=> $image_mime,
                    'contents' => fopen( $image_path, 'rb' ),
                ],
            ]
        ]);

我还尝试创建一个GuzzleHttp\Psr7\Request实例。不幸的是,json_encodeUploadedImage的使用无济于事,因为编码对象将转换为NULL。我应该将正文转换为Stream吗?如果是这样,那将如何工作?

$request = new Request($method, $uri, $headers, $body);

有什么我想念的吗?

0 个答案:

没有答案