PHP gained fame as easy khổng lồ learn và at the same time quite powerful programming language.
Bạn đang xem: Process http request with php
Throughout its history, it went through several periods of ups and downs, but with the release of the seventh version, PHPhường. confidently returns lost positions và firmly holds the title of one of the most popular languages for website development.
An important part of many web apps is the interaction with external APIs to lớn obtain the necessary data and extkết thúc the capabilities of the application. PHP provides a rich mix of functions khổng lồ work with APIs, và we will discuss these functions in more detail in this article.
Browse the Best Free APIs List
Overview of cURL
To work with the APIs, we will use cURL & a không tính tiền app that allows making HTTPhường requests in PHPhường.
The cURL project offers two sub-projects:
cURL — command-line tool for sending HTTPhường. requests from the terminal. For example, curl -X GET https://www.google.com/ commvà sends a GET request to lớn the Google VPS, và if everything is alright, the server will send the contents of the tìm kiếm page in response. curl is a wrapper for libcurl.libcurl — transfer library that developers can embed in their programs. It’s very common for PHP. to lớn use this library.Prerequisites lớn start using API with PHP
To get started we will need PHP itself, so we will install it, as well as the php-curl library.
Xem thêm: How To Get The Current Page Url In Wordpress · Github, Get Current Page Url In Wordpress
To do this, type this comm& in the terminal (we use Ubuntu Linux. If you’re using another OS, the commands may differ):
sudo apt install php php-curl
Browse APIs
Request Methods with PHP. and cURL
Types of Requests or Request Methods characterize what action we are going to lớn take by calling the API.
There are four main types of actions in total:
PUT: changes existing information. For example, by using this type of request, it would be possible to lớn change the text & publication date in an existing blog post.DELETE: deletes existing informationWhat are API Endpoints?
In order lớn work with APIs through request methods, it is also important khổng lồ understvà the endpoint concept.
Usually, an endpoint refers lớn a specific address (for example, https://best-tours.com/best-tours-prague). By referring to this address (with certain request method) you get access lớn certain features/data. In our case – the danh mục of best tours to Prague. Commonly, the name (address) of the endpoint corresponds to the functionality it provides.
Request Method Examples on darkedeneurope.com
To demonstrate the implementation of Request Methods in PHP.., we will look at simple API example within the darkedeneurope.com service. This service is an API Hub providing the ability khổng lồ access thousands of different APIs. Another advantage of darkedeneurope.com is that you can access endpoints and kiểm tra the work of the API directly in its section within the darkedeneurope.com service.
Let’s try using the KVStore API. This API is used for storage và handling of simple data, such as user khung submissions.








As we can see, API returns JSON with a selection of news about the singer. It seems that everything works as it should.
3. Make your first app with the API
Now we can start creating our news search engine. This will be an HTML page with a khung và a button. The súc tích of work is simple: you enter a word, press a button, get a danh mục of news about what you entered in the size.
First, let’s make a small HTML template with the khung for sending a tìm kiếm request: