Skip Navigation

Setup Getting Images from Instagram with PHP

In this episode, we will use composer to install Guzzle HTTP and set up our initial file.

Transcript

So to get started, I'm going to assume you have basic understanding of PHP. You already have a PHP environment set up. If you haven't set up a PHQ environment, definitely watch one of these previous series on Cote time, maybe the Laravel valet one or the, uh, map one for setting up a PHP environment. Now that you have a php environment, you got some basic page P down. You're probably going to need composer. So if you're not familiar with composer, you can go and get

Closer here. Uh, it's just composer. I just, there we go.

Uh, it's just get composer.org. And, uh, it is a dependency manager for PHP. So otherwise watch the series on setting up composer, which is pretty straight forward, uh, to get you going and composing. So let's jump in and create our project. So I'm going to go into my builds directory and I'm going to make a new folder called

Instagram,

And that's going to be my project folder. So I'm going to open that up and sublime, and then here it is in the directory, and now I'm going to make a new file. I'm going to call it index stock PHP, and that's going to be the file that I'm going to work from. Next thing I need to do is actually install this package. So the package is called guzzle HTTP, and it is a, a course using composer. It is a requesting package. What that means is I can send out requests or post data to a service. So, uh, it could be a website. It could be another API, uh, anywhere basically. Uh, and that's what it just basically sending data out. And it's very simple to get set up and you can do quite a bit with it. So in this example, here, they show client equals new guzzle HTTP request.

So they're instantiating the PHP class. Then they're saying, take the client that they just instantiated here, which is a variable request to get meaning, to get request to this URL, to go and get that URL. Then they get the data status code back, which is 200, which is good. They get the headline content type. So it's Jaison type. And then they go in and say, Rez get body. And they actually get the data from it. So we're going to do something very similar to that, uh, to go and get our data from Instagram. So let's scroll down here and it shows you how to set up. So I'm assuming you have composer already installed. So then we just need to write this line right here, which is the require guzzle HTTP guzzle, and you can run it with the PHP composer dot far. If you have composer set up with an alias or globally there, you could just run composer space require. So I'm going to go ahead and do that. And I'm going to go into my, uh, builds directory via terminal, and then, uh, click on to the, uh, Instagram folder. So I'm going to go CD into Instagram.

I am, uh, there we go. And stuff, Graham. Yeah,

There we go. And then I'm going to say composer, and then I'm to pace this line here require guzzle. So composer require guzzle HTTP guzzle. And if you've worked or composer before, this is pretty straightforward, right? We're just changing the directory. And then we're installing composer. It's going to install the various packages that go along with composer here. The guzzle promise a HTTP message guzzle agg PPSR seven and the actual puzzle library. So that's set up and you'll see over here. Now in our file, we have a composer lock file, which is keeping track of our changes or composer, Jace on file, which is the actual packages that we might be using. When, whenever we say composer, install or composer or acquire, it's going to basically look here. And then all of the packages we do install get installed to a vendor folder. After here, we can run the acquire a vendor autoload on top of our index dot PHP page.

So we'll open up PHP, we'll say require vendor autoload and that's going to load any packages that are run in here. So we'll now be able to go and reference them. So here's a little bit about the versioning. And at this point, now, if we scroll up, we can run our first request and we can just grab this right here, if we wanted to, just to test it out. So I'm going to go. Client equals new HTTP, guzzle client Rez, client requests, the 200 that get body. And now we can go and test this. So again, I called mine Instagram as my folder. If you're running, man, it would be something like local host slash Instagram. If you're running in valet, it'd be something like instagram.dev. So I'm going to go in here to instagram.dev and there is it there's 200 application character site, Jason character, UTF eight there. And then here is our values as we go through and we can see it. So at this point now we have it set up. It shows that we just pulled some data over from this URL here. We're going to change this code up so that we actually pull our Instagram data. Instead of, of course this, uh, guzzle repo data.