What to expect in the next generation of Laravel Forge. Read the blog post
Rewatch this lesson
Courses
/
PHP Fundamentals
/
Your first PHP application

Your first PHP application

PHP Fundamentals

Your first PHP application
Build a complete CLI weather application, combining all learned concepts into a practical project.

Let's put everything we've learned into practice by building a simple command-line weather application. This project will combine many concepts we've covered: Composer, classes, modern PHP features, and working with external APIs.

What We're Building

We'll create a CLI app that shows the current weather for any city. When finished, you can run it like this:

php weather.php Vienna

The project includes:

  • Using Composer for dependency management

  • Working with external packages

  • Creating and using classes

  • Making API requests

  • Modern PHP syntax

This simple app demonstrates how different PHP features work together in a real project. It's a perfect stepping stone before diving into Laravel.

Want to explore the code? Check out the complete project on GitHub:

https://github.com/laravel/php-fundamentals-weather-app

00:00
For our final video, let's put everything we've learned into practice by building a simple command-line weather checker.
00:07
We'll use Composer packages, work with an external API, and create a real and hopefully useful application together.
00:14
In order to receive information about the current weather for a city, we're going to use the OpenWeather service.
00:21
It helps us to find the weather information about a specific city, like Vienna here.
00:26
And it has a nice API, which we can use for free up to 1,000 calls.
00:31
We're going to create this weather app from scratch, and we're going to use Composer in it to create this.
00:37
So I'm fine with this name and window and name.
00:41
And description, our first PHP application.
00:47
Author is correct, and we can skip here a few and use the default values.
00:52
And yes, we would like to use some dependencies.
00:55
We are going to use just one here, and we're using Gazelle again.
00:59
And if we scroll up here, you can see 0 is the one which we're needing, Gazelle HTTP.
01:06
Let's pick 0 here. This is the one which we're going to use to call our weather API.
01:12
And we're going to use the latest version.
01:15
And we don't need any more dependencies and also no dev dependencies.
01:20
But we need auto-loading. Yes, that's fine.
01:23
And yeah, here we go. Let's also install our dependencies now.
01:26
Okay, perfect.
01:28
And here is our weather application opened up in PHPStorm in my editor of choice.
01:33
And here we have our Composer JSON file like we just created it.
01:37
We also have a Composer log file here.
01:39
When you install your dependency, this will be automatically created.
01:42
And this holds all the information about your package dependencies,
01:45
your package package dependencies, and all the different and exact versions
01:50
which are being used for this project currently.
01:53
And then we have a window folder with the code of our dependencies.
01:57
And then we have a source directory.
02:00
And inside the source directory, we're going to start by creating our first PHP class.
02:05
And we're going to call this WeatherService.
02:09
All right, here we go.
02:11
So this will be the main class which we'll use in order to talk to the API
02:15
and then return information that we get back.
02:19
So we're going to start by creating a constructor.
02:21
And now let's think about what are the things that we need here.
02:25
And I'm going to make those values private
02:27
because we are probably not going to need to use them from the outside.
02:30
And yes, we need an API key.
02:33
So we're going to set this here directly.
02:36
I have already prepared this one from our WeatherService.
02:39
And then we're also going to need an API URL, right?
02:43
Let's check the endpoint.
02:45
Yep, that's right. That's exactly the one which we need.
02:48
And then we also need to work with the Gazelle HTTP client.
02:52
And we can set this as a property.
02:54
So normally we've seen that we can use string, integer, or Booleans
02:58
as type for our properties.
03:00
We can also say this should be of the type of the client class.
03:04
And here we're creating this property which we don't set here,
03:08
but we do this inside the constructor exactly like this.
03:12
So we're saying this client which gets me this property
03:16
should now get a new instance of the client.
03:18
We don't need any arguments here.
03:20
And we can end this statement.
03:22
phpStorm is only complaining that we haven't used them yet.
03:25
But by the way, we can also make those two values read only.
03:29
But yeah, again, we are providing those here through the constructor
03:32
using constructor property promotion
03:34
because we can do this right in the constructor.
03:36
But with the client, which the instance which we have to set,
03:39
we need to mention this above here separately.
03:43
Okay, so far so good.
03:45
Now we need to add here a public function.
03:48
So this will be the function that we then can call from the outside.
03:51
And we're going to call this getWeather.
03:54
And we want to receive here the city, which is a string.
03:58
And then in the end, we want to return an array from this function.
04:02
All right.
04:03
And now we want to talk to the API and get information about a specific city.
04:08
So we're going to make use of our client here,
04:11
which has a get method to make a get request.
04:14
Then we're providing our API URL.
04:16
This is correct.
04:17
And then we have here some arguments that we can pass.
04:21
And the one that we want to set is query.
04:24
I'm going to set this real quick.
04:26
And then we talk about what we have here.
04:28
So yeah, autocompletion is already pretty good here.
04:31
So we're going to say that we want to add this API URL
04:36
and add some query parameters to this URL.
04:39
And the way that this works,
04:40
this is just a fancy way of saying here we want to add the city, for example,
04:44
which would be do with QCity.
04:46
And then we maybe want to add the app ID, which is the API key and so on.
04:52
This is exactly the same.
04:53
We're just doing this here for PHP code because it is a little bit better to read.
04:58
So Q is our search term and we're searching for a specific city.
05:01
Then we're providing the API key.
05:03
It's called app ID on this weather service.
05:07
And then we are setting the units to metrics
05:09
because these are the only units that I can read.
05:12
Okay.
05:13
Then we're going to get your response back if the request is successful.
05:21
And now we want to get our weather data working here nicely.
05:26
So let's check what we're doing.
05:27
So we get a response back, which is an object.
05:30
So it's a specific instance.
05:31
Then we want to get the body.
05:33
We're calling this method.
05:34
And then on this, we're going to call the get content.
05:37
And this will give us back a string, which is JSON,
05:41
which we then decode in order to get an array.
05:44
And we're also using the second parameter true to get an associative array back.
05:49
Okay.
05:50
So inside our weather data variable, we now have all this information.
05:53
And then the only thing left is to return this from this method.
05:56
And I have already prepared this.
05:58
So we are returning an associative array with the key city temperature,
06:02
description, and humidity.
06:04
And here we're just getting this information from the weather data,
06:07
which we just got back from our API endpoint.
06:11
And this should be it for this class here.
06:14
This is looking good now.
06:16
Let's think about how do we want to call this.
06:19
So in the end, this will be a common line interface application.
06:22
So we want to run this from the console.
06:23
And the way that we are going to do this is very simple.
06:26
But we're saying we want to run for PHP as specific PHP file.
06:30
And then we're providing a city like Vienna.
06:33
So this is our end goal here.
06:35
So this means we need this weather.php file inside the root of our project.
06:40
So let's create a new PHP file.
06:42
Let's call this weather.php.
06:45
All right.
06:46
And inside here, we now want to make use of our weather service.
06:50
But before we can do this, we need to require the autoload file,
06:54
which Composer created for us.
06:56
So this makes sure that we have access to all the different classes
06:59
inside of our source directory,
07:02
which we want to make use of because we want to make use of our weather service here.
07:06
Okay, and now we create a new instance of our weather service.
07:10
New weather service exactly like this.
07:13
And we're defining a city.
07:14
We're doing this statically here for now.
07:16
I'm just saying this is Vienna.
07:20
And then we get the weather now by calling the public get weather method
07:25
of our weather service here.
07:27
And we provide the city.
07:29
And let's just dump out the weather here so that we can see if it's working.
07:33
Let's run this already.
07:34
And yeah, you can see we already get some information back.
07:37
It's not very nicely to read, but it is there.
07:40
So that means it's already working.
07:42
So we've made a successful call to a specific API.
07:45
We get some data back, and we're going to return it here to our comment line,
07:49
which is already pretty cool when you think about it.
07:52
Let's just make this output a little bit nicer.
07:55
It looks a little bit more messy here, but the output will be nicer.
07:58
So we just have here some echo statements here in order to tell what's going on.
08:01
This is going to create a new line.
08:03
And then we have some structured information,
08:05
which we provide to the comment line.
08:09
If we run this now again, you can see, yeah, we're getting the weather for Vienna.
08:12
See this Vienna temperature is nicely here, clear sky, humidity 58.
08:17
So perfect condition for today's tutorial.
08:21
And yeah, when I think about it, this is already pretty cool.
08:23
It's working.
08:24
We've made use of an API.
08:26
We get some information.
08:27
We can run this through the comment line.
08:28
Just a few little things that I want to do here.
08:30
First, we don't want to define this here.
08:33
We want to define this through the console.
08:36
And as you've noticed, we haven't set this.
08:38
So this is a global variable, which is available,
08:40
which just gets the arguments through the console.
08:43
And we're just saying here we want to get the second argument,
08:46
because the first one is the weather PHP file,
08:49
and the second one is the city.
08:51
By the way, let's try it out.
08:52
It's still working.
08:53
Let's see if we change this now to Barcelona.
08:57
It's also working.
08:59
Thunderstorm with light rain.
09:00
Oh, so we have better weather here in Vienna.
09:03
Good to know.
09:04
So yeah, pretty cool.
09:05
We can now define this through the console.
09:07
And yeah, one thing that's now not working anymore.
09:10
If we forget to provide this, we will get this messy error
09:13
when we try to make use of city, which is now not a string,
09:17
which is null.
09:18
And the way that we can prevent this is also quite nicely.
09:22
We can just say if we have another argument, RxC.
09:26
So this gives us the amount, the count of how many arguments we got,
09:30
and if it's smaller than two.
09:31
So we want to make sure we get at least two arguments,
09:34
the PHP file and the city.
09:36
And if it's smaller, then we want to echo out.
09:40
Let's say correct usage is phpweather.php.city.
09:48
All right, and then we want to exit after that
09:51
because we don't want to move on with the rest of the code,
09:54
which we can do with the exit method.
09:56
Okay, let's try now again.
09:59
Yeah, now we can see this message here.
10:02
And if we use city again, let's go for Chicago.
10:05
You can see it's back working.
10:08
Awesome.
10:09
So that's it for your first PHP application for our web application here.
10:13
But of course, yeah, there are a few things that we could or should improve.
10:16
So for example, it's not a good idea to use API keys directly in your code.
10:21
You normally would include them through an environment variable.
10:24
That's one thing.
10:25
Then also there are really nice frameworks out there to build
10:28
beautiful command line interface application,
10:31
something like we did here, but with nicer output and nicer input.
10:35
So that's also a good idea to check them out as well.
10:38
And then it's also a good idea to handle exceptions.
10:40
So phpStorm is telling us here that there might be a guzzle exception,
10:44
and it's also a good idea to handle this.
10:47
But yeah, that's something that you can still look into
10:49
and try to improve for yourself.
10:51
Thank you for completing this PHP fundamentals course.
10:55
You now have a solid foundation in modern PHP
10:58
from basic syntax to classes and composer.
11:01
But PHP truly shines when building web applications,
11:05
especially with frameworks like Lerval.
11:07
So Lerval takes all these PHP concepts we've learned
11:10
and provides a powerful toolkit for building everything
11:13
from a simple website to a complex web application.
11:17
Are you ready to continue your journey?
11:20
Check out our next course about getting started with Lerval.