Traditional Culture Encyclopedia - Weather inquiry - Introduction to Docker PHP (3)
Introduction to Docker PHP (3)
Choose high-tech open platform-weather query API mainly because it is free. Of course, you can also use other third-party weather query interfaces to see your personal preferences.
This application is a very simple REST API application, which mainly implements two interfaces.
Before coding the application, we use Docker to install and run ThinkPHP.
ThinkPHP is a free and open source, fast and simple object-oriented lightweight PHP development framework, which is born for agile WEB application development and simplified enterprise application development. ThinkPHP has been adhering to the simple and practical design principle since its birth, paying more attention to ease of use while maintaining excellent performance and minimum code. Following the Apache2 open source license agreement means that you can use ThinkPHP for free, and even allow your ThinkPHP-based applications to be open source or commercial products to be published/sold.
This is why I chose it as the teaching framework of this course. I don't want you to give up because of a framework, but I don't want to build everything from scratch, because the focus of this tutorial is Docker, not our PHP application.
Creating ThinkPHP applications with Docker actually requires less operations than configuring the PHP environment locally. And because we still need to use Composer, thanks to Docker, we don't even need to install it on the host.
First, open your terminal and create a project directory.
And enter the directory.
Now use [official Composer Docker image] (/_/composer/) to install ThinkPHP.
If you check the weather-app/ directory, you will see the project directory of ThinkPHP 6, as shown in the following figure:
Our docker run command is similar to that in Chapter 2, but we use a different image. Instead of using a PHP image running hello.php scripts, we used a Composer image. Let's see what has changed.
After creating the project, we need to add several routing URLs and controller files. Let's open the application/controller in the weather-app directory and create a new Weather.php file with the following contents:
Then open the application/route in the weather application directory and add the following contents to the app.php file:
Now we can run our application in the Docker container, just to verify whether our application works properly, because we only added two routing URLs. Open the command line and run it.
Now, the two commands to open poser create-project ... are different in the browser. The reason is that this time we want to get the latest version of PHP containing Apache, so that we can provide services for our Web applications. Let's learn more about the new command section.
You can stop and exit the terminal by sending an "interrupt" signal to the terminal. On windows, this can be done by pressing Ctrl and C.
Another option to run a new web application is to run the container in detached mode. This means that you can't see the output of the container at the terminal. This can be achieved by adding the -d flag to the previous command.
After starting the container in detached mode, your terminal will display the complete ID of the new container-similar to a 70d 25 C2 a 7 cede 673 F8 ab ... If you want to stop the container, you can use the docker stop command and tell docker with the container ID. take for example
Because it is troublesome to enter the whole ID, Docker allows you to enter only the first three or more characters, if you want.
Finally, I suggest naming your container. We will do this in many examples later in this book, because it is much easier to remember the container by name than by randomly assigned ID, and the ID is random, so every time a new version of the container is run, it will get a new ID. As long as there is no container with the same name, you can issue the name multiple times. In order to name the new application container, we can recreate it with the passed-in name tag.
There are many options available when using the docker run command, so you may want to read the documentation in more detail. When we develop the remaining applications, we will cover some of these options.
Now I want to introduce Gaud's weather SDK. Before using this SDK, you need to read the technical document of Gaud open platform-weather query. Before adding SDK, we must first ensure that all existing containers are stopped.
This command will list all running containers. You can also see the stopped containers by adding the -a flag.
If there are any containers running, use docker stop to stop them before we continue.
This command will install new software packages in your project. During this process, you should see output similar to this in the terminal.
Now the SDK has been installed and can be used.
We will use the newly added Gaode Weather SDK to improve our business logic, and open Weather.php in the controller directory to add the following contents:
We have made some updates-mainly by introducing the weather API to initialize the weather class.
Our application has initially finished passing a real location ID to the API and returning some data. First, use this high-tech location query to find the location ID. I used the ID of Shanghai for the test. 3 10000 can of course be sent directly to Shanghai. Ok, let's run the Docker container again.
And access the running application in your browser at http://localhost: 38000/weather-app/public/index.php/weather/310000. You should be able to see JSON data as shown below.
Your Docker PHP application now returns real data from external data sources and provides services with Apache, but you may notice that its speed is not fast (my page loading time is 1.92 seconds! )。
Gaode Weather API is a free service, which may not be available in other countries. In order to solve this problem, we will save the query data in our MySQL database so as to respond quickly the next time we visit. This will greatly improve performance. In the next chapter, we will learn how to combine MySql with PHP applications with Docker.
- Related articles
- What are the details of the total solar eclipse on July 22nd? Can you see it in Fushun, Liaoning?
- Talking about the classic QQ mood-lighting a cigarette and recalling the past.
- Why pay attention to the weather forecast?
- What to wear for a trip to Qiandao Lake in Hangzhou?
- What are the commonly used mobile phone software?
- When is the best season to go to Turkey?
- Xiaogan weather forecast
- Why don't you take an umbrella when it rains?
- What is the climate like in Canada?
- What is the Christmas stalk of Changjin Lake?