Upload image to external webservice/API using Lumen/Laravel and Guzzle 6

萝らか妹 提交于 2021-02-11 16:35:00

问题


I want to upload an image to a webservice using Lumen + Guzzle 6. The external web service does not accept "base64-encoding" the image to be sent. Only the image file is accepted as such.

But I still get an error message from the external webservice/API that the image could not be found or it rather seems to be that Guzzle sent the request without the image. The check with file_exists($filename_with_path) responded successfully, therefore access to the file is generally possible.

If I work with cURL-command-line on the console, it worked fine for me:

curl -v --user "username:password" ...

Thank you very much for every hint.

Here is a bit of the problem-code:

$filename_with_path  = storage_path( 'app/pics/') . $file_name;        
$fileContent         = File::get( $filename_with_path );
$mime_type           = File::mimeType( $filename_with_path );
$this->guzzle_client = null;
$this->guzzle_client = new Client( [
    'auth'      => [
        'username',
        'password'
    ],
    'multipart' => [
        [
            'name'      => $file_name,
            'filename'  => $file_name,
            'contents'  => $fileContent,
            'mime-type' => $mime_type,
            'headers'   => [ 'Content-Type' => $mime_type ]
        ],
    ],
] );

// send
$this->response = $this->guzzle_client->request( 'PUT', $base_url . $endpoint ); 

来源:https://stackoverflow.com/questions/60882734/upload-image-to-external-webservice-api-using-lumen-laravel-and-guzzle-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!