Skip Navigation

Looping through the Data Getting Images from Instagram with PHP

In this episode, we will loop through some data and display the images that we have requested.


Okay. So in the last video we set up this URL, Greenland. If we pass it to our guzzle request again, look how easy that is. You just send a get request. You could also send a post request with some post data with guzzle. It's very easy to set up and get started with. So would Jason decode that data? And we get the data back here, then we have this print, our data, and we're seeing that here. And what I've done is I've zoomed out of my browser window with command minus. So I can just see the, the, the entire length of this object, or at least most of it. And what I see here is this pattern with this three here. And then if I scroll up on that same line, I get it two, and then I get one and then I'll get zero.

And then this is on side of items with just inside of this object here. So if I wanted to go in, get to the items, I could use the object syntax here by saying items. And then if I print our hair and watch that array, you'll see that all kind of just pushed up. And then if I want to grab this first one, I could go and reference zero on that. So I'm going to use the erase and tags here for zero Oh, it's actual zero, not the string zero. And now I'll just get that individual object. So that individual object is going to come with a bunch of stuff. As you can see here, it has the light. Yikes, um, bunch of people, things in their images here likes created time, the user who originally posted it, the code, the idea of it, uh, down here, we have comments.

Didn't get any comments, looks like comment, view comments. Here can delete comments. These are just some options, the link for it. Yeah. And here is some likes. So I guess these are the people who actually liked the post. So I'd be able to pull their profile page and their usernames. Okay. So at this point I'm really after is the pictures, right? I mean, that's the whole game, right? So we want the pictures. So here's this object. We can go and reference the pictures inside of this one object by saying images. And then inside of that, which is an object I could go in there and say standard resolution, or I could do the thumbnail. I'm going to start with the thumbnail because I think that'll be the easiest one. Well, I guess they're all the same, but I just see that one. And we're just starting, it's a smaller size picture as well.

So I'm going to go and say inside this, let's go and reference, uh, images. Let's see if that works for us. So now we're inside images and then inside images, let's go and reference thumb. Yeah. Now, so I'm going to say thumbnail and now Howard, just at the thumbnail, then we can reference URL here and now we'll get just that URL. So if I was to grab this URL and I'm going to make this a little bit larger, so you can see what's going on. If I was to grab this, that URL and open it up and paste it in a new window, you're going to see there's that cat photo right there of, uh, the picture that I posted. So, um, pretty easy, right? So now we kind of get the base of how this can be structured. Now we can learn looping through of that data with a, for each loop.

So to get started with that, that's where the, uh, the items right here is basically where the, the images start. So what I can do here is say images equal, uh, data items. And that's going to be the same as images now in my mind, because it's each image object where we referenced a zero. This is now going to be one, two, three, four, five, six, however many images I have. So now I can loop through that by using a, for each loop and saying for each images as, and then I'm going to reference as individual image. Now that at this point, right here is this instance. So this would be the same as this now, right? Then from this image object here, I could go and reference the image images cause there's multiple size images and get me the thumbnail images or thumbnail URLs.

Now, if I just wanted to see that value, I had echo out that value like, so, so now I can comment this line out using the command forward slash and sublime. If I go back to this page and click refresh, you'll see, I just have a really long, it's actually just a really long URL string of URLs going across, right. Just keeps going all the way across. So instead of it doing that, I'd actually like to see images instead of this, the URLs. So let's go ahead and concatenate that onto an image tag. So to get started with that, I'm just going to use the single quotations here, and I'll say image SRC, and then I'm going to use the big quotation for the SRC. And then That's where we're going to concatenate this value that we've set up here into the value here.

Now, if you want, it's always easier course to set up a variable and then just reference, I'm going to call this thumbnail equals, and then I can grab the thumbnail and concatenate the thumbnail here, and then a single quotation and a double quotation, and then let's close it and put a semi-colon. So that's our base here. Image source, thumbnail, a single quotation, double quotation. And then we close it. Now, if we refresh this, there are all the images from my Instagram count, those, those top sort of images. So I think they're the top one, two, three, four, five, six, seven, eight, nine, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, maybe 19 or 20, uh, images. So, uh, which is great. There they are. I got them. You can see that. They're definitely the thumbnail size. If I wanted to go and get the larger size, I can just come back to this.

If I print it are where the images are and I can comment this out. Then I can come back and reference and you'll see here, my thumbnail, my low Rez am my standard, the resolution, and that's at six 40 by six 37. Looks like. So then I can come in here where it says thumbnail size or comment this back, instead of saying thumbnail, I'm going to say standard resolution. And it's still now, if I refresh, I'll see they're much, much larger, right? Look how big that is in comparison. I'm really zoomed in, of course that's at actual full screen and you're seeing that sharpness there, the images at the full size of the Instagram photos, of course here is the, uh, print are that it's printing out. Uh, so pretty cool. We're grabbing the images very easy to go in and grab them and loop through them.