Search code examples
phplaravelresponseimgur

How to get a specific value of response result


I'm trying to make an image upload to Imgur with Laravel through a Laravel controller, however, I have already created the upload but I don't know how to get the specific Id of the response results. Thank you.

$client = new \GuzzleHttp\Client();
        $response = $client->request('POST', 'https://api.imgur.com/3/image', [
            'headers' => [
                'authorization' => 'Client-ID ' . 'my_id',
                'content-type' => 'application/x-www-form-urlencoded',
            ],
            'form_params' => [
                'image' => base64_encode(file_get_contents($request->file('image')->path()))
            ],
        ]);
        echo $result = response()->json(json_decode(($response->getBody()->getContents())));

the Value that I want to get


Solution

  • -> can be used to access object properties. Checkout the following example:

    $client = new \GuzzleHttp\Client();
            $response = $client->request('POST', 'https://api.imgur.com/3/image', [
                'headers' => [
                    'authorization' => 'Client-ID ' . 'my_id',
                    'content-type' => 'application/x-www-form-urlencoded',
                ],
                'form_params' => [
                    'image' => base64_encode(file_get_contents($request->file('image')->path()))
                ],
            ]);
            $response = json_decode($response->getBody()->getContents()));
            // get the results:
            $id = $response->data->id;
            $height = $response->data->height;