mardi 19 mars 2019

How to upload an image with Laravel using an API?

I am trying to upload a single image using an API in Laravel controller. I have tested the API in postman and it uploaded the image successfully. However, when I come to upload it in Laravel I get 500 server error. Here is my controller code.

public function uploadImages($id, Request $request)
{
    $client = new Client();

    $file = $request->file('images');
    $file_name = $file->getFileName();
    $file_path = $file->getPathname();
    $file_mime = $file->getMimeType();
    $file_org = $file->getClientOriginalName();


    $response = $client->post('http://127.0.0.1:5111/api/upload?hotelid='.$id.'&main=0', [
        'auth' => [
            'admin', 
            'a1@dmin'
        ],
        'multipart' => [
            [
                'name'     => 'image',
                'filename' => $file_org,
                'Mime-Type'=> $file_mime,
                'contents' => fopen( $file_path, 'r' ),
            ],
        ]
    ]);

    echo $response->getBody();   
    //$xml_string = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $response->getBody());
    //$hotel = simplexml_load_string($xml_string);
    //var_dump($response);

    return redirect()->back();

}

What is it I am doing wrong in this code. Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire