I
I
Ivan Vorobei2016-06-11 17:33:10
Laravel
Ivan Vorobei, 2016-06-11 17:33:10

POST image from Laravel?

First, an API request comes in , with an image attached to it.
828c4009850e4fffb9f0023ee3f47327.png
Next, in the controller, I have to send this image using the POST method to another API.
In the controller, I receive the file, and with the help of the SDK from the second API, I send it. The documentation says that you need to send the image using the POST method.
I use
$image = Input::file('img')
BUT I get not an image, but apparently a path. Here's what's in the image variable : /tmp/phpxtlLt4
A function from the SDK to which I pass the image (in this example, image):

$params = ["img" => '@'.$image]
$params = array_merge($this->auth_params, $params);
        $url = $this->api_server_url . "$method";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        curl_close($ch);

        $result = null;
        if (!empty($data))
        {
            $result = json_decode($data);
        }

        return $result;

What am I doing wrong?
If I make a request directly from Postman - everything is in order.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-06-11
@ivanvorobei

In this case, $image is an UploadedFile instance that inherits from SplFileInfo , which in turn has a __toString method that returns the path to the uploaded file.
You can not read the file, but simply replace:
$params = ["img" => '@'.$image->getPathName()];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question