/    Sign up×
Bounties /Pin to ProfileBookmark

I use php curl to get html table from other web and convert it to json.

I use php curl to get html table from other web and convert it to json. but when displaying it got text error. it is encrypted. please solution. Thanks

<?php
header('Content-Type: application/json;');
$url = 'https://www.cskh.evnspc.vn/TraCuu/GetThongTinLichNgungGiamMaKhachHang?tuNgay=29-05-2023&denNgay=04-07-2023&ChucNang=MaDonVi&madvi=PB0501';

// Khởi tạo một phiên cURL
$ch = curl_init();

// Thiết lập URL của trang web cần lấy HTML
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// Thực hiện yêu cầu cURL và lấy HTML từ trang web
$html = curl_exec($ch);
// Kiểm tra nếu có lỗi trong quá trình yêu cầu cURL
if(curl_errno($ch)) {
    echo 'Lỗi: ' . curl_error($ch);
}

// Đóng phiên cURL
curl_close($ch);

// Tạo đối tượng DOMDocument và tắt các cảnh báo lỗi HTML
$dom = new DOMDocument();
libxml_use_internal_errors(true);

// Load chuỗi HTML
$dom->loadHTML($html);

// Tìm các phần tử table, thead, tbody
$table = $dom->getElementsByTagName('table')->item(0);
$thead = $table->getElementsByTagName('thead')->item(0);
$tbody = $table->getElementsByTagName('tbody')->item(0);

// Lấy danh sách tiêu đề cột từ thead
$headers = array();
foreach ($thead->getElementsByTagName('th') as $header) {
    $headers[] = $header->textContent;
}

// Lấy dữ liệu từ tbody
$rows = array();
foreach ($tbody->getElementsByTagName('tr') as $row) {
    $rowData = array();
    foreach ($row->getElementsByTagName('td') as $key => $cell) {
        $rowData[$headers[$key]] = $cell->textContent;
    }
    $rows[] = $rowData;
}

// Gán dữ liệu vào mảng chính
$data['items'] = $rows;

// Chuyển đổi thành JSON
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
// In ra kết quả
echo $json;
?>
to post a answer
HTMLPHP

3 Replies

Davinci•003 auto-replyYou can use the PHP function json_encode() to convert an HTML table to JSON. Here is an example of how to do this:


// Get the HTML table
$html = file_get_contents('http://example.com/table.html');

// Create a DOM object
$dom = new DOMDocument();

// Load HTML
@$dom-&gt;loadHTML($html);

// Parse the HTML table
$tables = $dom-&gt;getElementsByTagName('table');

// Convert the HTML table to an array
$table_array = array();
foreach ($tables as $table) {
$rows = $table-&gt;getElementsByTagName('tr');
foreach ($rows as $row) {
$cols = $row-&gt;getElementsByTagName('td');
$row_array = array();
foreach ($cols as $col) {
$row_array[] = $col-&gt;nodeValue;
}
$table_array[] = $row_array;
}
}

// Convert the array to JSON
$json = json_encode($table_array);

Was this helpful?

Copy linkTweet thisAlerts:
@harrydang-03authorJun 29.2023 — Inactive
Copy linkTweet thisAlerts:
@oussamaJul 02.2023 — The text error you are encountering is most likely due to the fact that the HTML you are retrieving is encoded or encrypted. To resolve this issue, you need to identify the encoding of the HTML and decode it before parsing it as an HTML document. Here's an updated version of your code that includes decoding the HTML before loading it into the DOMDocument:
×

Success!

Help @harrydang-03 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...