Menu
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;
?>