Làm cách nào để lấy dữ liệu JSON từ API trong PHP?

JSON (Ký hiệu đối tượng JavaScript) là một định dạng trao đổi dữ liệu nhẹ. Nó dễ dàng được đọc và viết bởi con người và được máy móc phân tích cú pháp và tạo ra.

$ curl localhost:8000
John Doe is a gardener
8 là loại phương tiện Internet chính thức cho JSON. Phần mở rộng tên tệp JSON là
$ curl localhost:8000
John Doe is a gardener
9

Hàm

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
0 trả về biểu diễn JSON của giá trị đã cho.
[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
1 lấy một chuỗi được mã hóa JSON và chuyển đổi nó thành một biến PHP

Các framework PHP như Symfony và Laravel có các phương thức tích hợp hoạt động với JSON

Mã hóa PHP JSON

Trong ví dụ sau, chúng ta sử dụng hàm

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
0

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php

Chúng tôi khởi động máy chủ và định vị tới

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
3

Làm cách nào để lấy dữ liệu JSON từ API trong PHP?
Nhân vật. Chế độ xem JSON trong Firefox

Các trình duyệt web hiện đại hiển thị dạng xem JSON cho dữ liệu JSON khi chúng nhận được loại nội dung phù hợp trong tiêu đề của phản hồi

Trong ví dụ sau, chúng ta sử dụng hàm

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
1

________số 8_______

Chúng tôi bắt đầu máy chủ

$ curl localhost:8000
John Doe is a gardener

Chúng tôi gửi một yêu cầu NHẬN với curl

PHP JSON được đọc từ tệp

Trong ví dụ sau, chúng tôi đọc dữ liệu JSON từ một tệp

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]

Đây là dữ liệu JSON




    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


Trong ví dụ về mã, chúng tôi đọc tệp bằng

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
5 và giải mã nó thành một mảng PHP bằng
[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
1. Sau đó, chúng tôi đặt dữ liệu vào một bảng bằng cách sử dụng vòng lặp PHP foreach

Trong ví dụ sau, chúng tôi đọc dữ liệu từ cơ sở dữ liệu SQLite và trả về dưới dạng JSON

BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;

CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;

Mã SQL này tạo bảng

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
7 trong SQLite

$ sqlite3 test.db
sqlite> .read cities.sql
sqlite> SELECT * FROM cities;
1|Bratislava|432000
2|Budapest|1759000
3|Prague|1280000
4|Warsaw|1748000
5|Los Angeles|3971000
6|New York|8550000
7|Edinburgh|464000
8|Berlin|3671000

Với công cụ dòng lệnh

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
8, chúng tôi tạo cơ sở dữ liệu SQLite và tạo bảng
[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
7

query('SELECT * FROM cities');
$cities = [];

while ($row = $res->fetchArray()) {
    $cities[] = $row;
}

header('Content-type:application/json;charset=utf-8');
echo json_encode(['cities' => $cities]);

Trong ví dụ, chúng tôi lấy dữ liệu từ cơ sở dữ liệu và trả về dưới dạng JSON

API tìm nạp PHP JSON và JS

Trong ví dụ sau, chúng tôi sử dụng API tìm nạp JavaScript để lấy dữ liệu JSON từ tập lệnh PHP

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]

Dữ liệu JSON được lưu trữ trong một tệp

Quảng cáo

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
0

Chúng tôi có một nút trong tài liệu. Khi chúng ta nhấp vào nút, hàm




    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


0 sẽ lấy dữ liệu JSON từ tập lệnh



    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


1. Một bảng HTML được tạo động và chứa đầy dữ liệu

HTML table filled with JSON data from fetch APINhân vật. Bảng HTML chứa đầy dữ liệu JSON từ API tìm nạp

Trong ví dụ sau, chúng tôi trả về dữ liệu JSON từ ứng dụng Slim

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
1

Chúng tôi cài đặt các gói




    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


2,



    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


3 và



    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


4

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
2

Chúng tôi sử dụng phương pháp




    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


5 của



    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


6 để trả về dữ liệu JSON

PHP JSON trong Symfony

Trong ví dụ sau, chúng tôi gửi phản hồi JSON từ ứng dụng Symfony

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
3

Một ứng dụng mới được tạo

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
4

Chúng tôi cài đặt các thành phần




    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


7,



    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


8 và



    
        
            Name
            Occupation
            Country
        
        
        
             name; ?> 
             occupation; ?> 
             country; ?> 
        
        
    


9

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
5

Chúng tôi thiết lập URL cơ sở dữ liệu cho cơ sở dữ liệu SQLite

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
6

Chúng tôi tạo cơ sở dữ liệu

quảng cáo

src/Command/CreateCityTable. php

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
7

Đây là lệnh tạo bảng

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
7 trong cơ sở dữ liệu

Quảng cáo

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
8

Lệnh được thực hiện

src/Kho lưu trữ/Kho lưu trữ thành phố. php

The example transforms a PHP array into a JSON string.

$ php -S localhost:8000 encode.php
9

Trong

BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;

CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;
1, chúng ta có phương thức
BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;

CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;
2 truy xuất tất cả các hàng. Chúng tôi sử dụng Doctrine DBAL để thực hiện truy vấn cơ sở dữ liệu

quảng cáo

src/Bộ điều khiển/Bộ điều khiển thành phố. php

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
0

Bên trong bộ điều khiển, chúng tôi tìm nạp tất cả dữ liệu bằng cách sử dụng

BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;

CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;
1. Dữ liệu được chuyển đổi thành JSON với trình trợ giúp
BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;

CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;
4

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
1

Chúng tôi khởi động máy chủ web và định vị tới

BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;

CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;
5

PHP JSON trong Laravel

Trong ví dụ sau, chúng tôi gửi phản hồi JSON từ ứng dụng Laravel

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
2

Chúng tôi tạo một ứng dụng Laravel mới

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
3

Chúng tôi xác định kết nối và tệp cơ sở dữ liệu

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
4

Chúng tôi tạo tập tin cơ sở dữ liệu

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
5

Một lần di chuyển mới cho bảng

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
7 đã được tạo

quảng cáo

database/migrations/2020_07_25_101535_create_cities_table. php

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
6

Trong tệp di chuyển, chúng tôi tạo một lược đồ cho bảng

[
    {"name": "John Doe", "occupation": "gardener", "country": "USA"},
    {"name": "Richard Roe", "occupation": "driver", "country": "UK"},
    {"name": "Sibel Schumacher", "occupation": "architect", "country": "Germany"},
    {"name": "Manuella Navarro", "occupation": "teacher", "country": "Spain"},
    {"name": "Thomas Dawn", "occupation": "teacher", "country": "New Zealand"},
    {"name": "Morris Holmes", "occupation": "programmer", "country": "Ireland"}
]
7

Quảng cáo

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
7

Chúng tôi chạy di chuyển

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
8

Chúng tôi tạo một trình gieo dữ liệu cho cơ sở dữ liệu của chúng tôi

cơ sở dữ liệu/hạt giống/CitySeeder. php

The example transforms a JSON string into a PHP variable.

$ php -S localhost:8000 decode.php
9

Seeder chèn tám hàng vào bảng

Quảng cáo
$ curl localhost:8000
John Doe is a gardener
0

Chúng tôi thực hiện seeder

$ curl localhost:8000
John Doe is a gardener
1

Bên trong tuyến đường, chúng tôi chọn tất cả dữ liệu từ cơ sở dữ liệu và trả về dưới dạng JSON với trình trợ giúp

BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;

CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;
4

$ curl localhost:8000
John Doe is a gardener
2

Chúng tôi chạy máy chủ web

$ curl localhost:8000
John Doe is a gardener
3

Chúng tôi lấy dữ liệu bằng công cụ

BEGIN TRANSACTION;
DROP TABLE IF EXISTS cities;

CREATE TABLE cities(id INTEGER PRIMARY KEY, name TEXT, population INTEGER);
INSERT INTO cities(name, population) VALUES('Bratislava', 432000);
INSERT INTO cities(name, population) VALUES('Budapest', 1759000);
INSERT INTO cities(name, population) VALUES('Prague', 1280000);
INSERT INTO cities(name, population) VALUES('Warsaw', 1748000);
INSERT INTO cities(name, population) VALUES('Los Angeles', 3971000);
INSERT INTO cities(name, population) VALUES('New York', 8550000);
INSERT INTO cities(name, population) VALUES('Edinburgh', 464000);
INSERT INTO cities(name, population) VALUES('Berlin', 3671000);
COMMIT;
9

Symfony cung cấp thành phần

$ sqlite3 test.db
sqlite> .read cities.sql
sqlite> SELECT * FROM cities;
1|Bratislava|432000
2|Budapest|1759000
3|Prague|1280000
4|Warsaw|1748000
5|Los Angeles|3971000
6|New York|8550000
7|Edinburgh|464000
8|Berlin|3671000
0 cho phép chúng tôi tạo các yêu cầu HTTP trong PHP. Chúng tôi cũng có thể gửi dữ liệu JSON

$ curl localhost:8000
John Doe is a gardener
4

Chúng tôi cài đặt thành phần

$ sqlite3 test.db
sqlite> .read cities.sql
sqlite> SELECT * FROM cities;
1|Bratislava|432000
2|Budapest|1759000
3|Prague|1280000
4|Warsaw|1748000
5|Los Angeles|3971000
6|New York|8550000
7|Edinburgh|464000
8|Berlin|3671000
1

$ curl localhost:8000
John Doe is a gardener
5

Ví dụ gửi yêu cầu GET với tải trọng JSON

quảng cáo

src/Trình điều khiển/Chào điều khiển. php

$ curl localhost:8000
John Doe is a gardener
6

Trong

$ sqlite3 test.db
sqlite> .read cities.sql
sqlite> SELECT * FROM cities;
1|Bratislava|432000
2|Budapest|1759000
3|Prague|1280000
4|Warsaw|1748000
5|Los Angeles|3971000
6|New York|8550000
7|Edinburgh|464000
8|Berlin|3671000
2, chúng tôi xử lý dữ liệu JSON và tạo một tin nhắn văn bản, tin nhắn này sẽ được trả lại cho khách hàng

Làm cách nào để tìm nạp API JSON trong PHP?

Sử dụng dữ liệu từ JSON với PHP. Đầu tiên, để đi sâu vào việc JSON đó chỉ đơn giản là một chuỗi, chúng ta sẽ viết JSON thành một chuỗi PHP và áp dụng nó vào một biến có tên là $data . $data = '{ "tên". "Aragorn", "chủng tộc". "Nhân loại" }'; .

Làm cách nào để lấy dữ liệu JSON từ URL API trong PHP?

Bạn có thể sử dụng hàm file_get_contents() và hàm json_decode() của PHP. .
Tệp get_get_conents() get được sử dụng để lấy hoặc đọc tệp từ đường dẫn đã cho (Url hoặc Nguồn)
Hàm json_decode() của PHP, dùng để convert nội dung file JSON sang PHP Array

Làm cách nào để tìm nạp dữ liệu từ API trong PHP?

php // Bao gồm các lớp Yêu cầu và Phản hồi $url = 'https. //api. exoclick. com/v2/đăng nhập';

Làm cách nào để gọi API JSON trong PHP?

Tệp PHP được giải thích. .
Chuyển đổi yêu cầu thành một đối tượng, sử dụng hàm PHP json_decode()
Truy cập cơ sở dữ liệu và điền vào một mảng với dữ liệu được yêu cầu
Thêm mảng vào một đối tượng và trả về đối tượng dưới dạng JSON bằng hàm json_encode()