Leo
Leo

Reputation: 7420

Instagram API with Laravel 5.4

I am using an Instagram package vinkla to get the user data to my app.

Here is the code that I use in order to get the results:

public function instagram()
{
   $insta = new Instagram();

   $getUser = $insta->get('gopro');

   dd($getUser);
}

When I diedump the results I get this array on return:

array:20 [▼
  0 => {#205 ▼
    +"id": "1552323772757218591_28902942"
    +"code": "BWK9j8rlpUf"
    +"user": {#208 ▶}
    +"images": {#202 ▼
      +"thumbnail": {#203 ▶}
      +"low_resolution": {#214 ▶}
      +"standard_resolution": {#204 ▼
        +"width": 640
        +"height": 640
        +"url": "https://scontent-frt3-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/19624693_104174916895493_9107740182827761664_n.jpg"
      }
    }
    +"created_time": "1499271435"
    +"caption": {#207 ▶}
    +"likes": {#215 ▶}
    +"comments": {#222 ▶}
    +"can_view_comments": true
    +"can_delete_comments": false
    +"type": "image"
    +"link": "https://www.instagram.com/p/BWK9j8rlpUf/"
    +"location": {#223 ▶}
    +"alt_media_url": null
  }
  1 => {#224 ▶}
  2 => {#251 ▶}
  3 => {#278 ▶}

]

Now the thing that I am trying to approach: I want to integrate some images on my laravel app. Basically I want to get the image url from each array images->standard_resolution->url.

I have tried to iterate the array with double foreach no luck so far.

Any suggestion on how to achieve that ?

Upvotes: 3

Views: 1965

Answers (2)

xmhafiz
xmhafiz

Reputation: 3538

use map from laravel collection will simply get the urls

$data = collect($getUser)->map(function($v) {
    return $v->images->standard_resolution->url;
});

And you also can use foreach

$urls = [];
foreach ($getUser as $row) {
    array_push($urls, $row->images->standard_resolution->url);
}

dd($urls);

Upvotes: 3

FULL STACK DEV
FULL STACK DEV

Reputation: 15951

Hi you may be getting the results in array you can convert the array to collection there is nice helper in laravel

   $yourCollection =  collect(yourArray);

then you can apply queries on Collection same as Eloquent and also apply the foreach loop.

Edit 2

You can do this also by

 $yourArray = json_decode($getUser,true);

you can do apply a loop on it.

@foreach($yourArray as $arr){
    //do what ever you want to do with
   $arr['standard_resolution']['url'];
  }

I hope this helps.

Upvotes: 0

Related Questions