JSON encoding problem output from PHP

Asked 2 years ago, Updated 2 years ago, 93 views

I am a high school student who is currently developing a KakaoTalk meal chatbot.

NEIS creates a JSON file (meal.json) through a PHP file (test.php) that parses meal tables, and this file is another PHP file (message).We are implementing a logic that reads from php) and finally outputs it in the form of a variable. (For each update, the test.php file is refreshed every 60 seconds, and the new content is written to the mill.json file.)

I'm working on putting all the files on Heroku (php7.1.13).

Currently, if you check the mill.json made in test.php as below, the Korean alphabet appears broken.

test.php

<?php
/**
* * meal_api.php
* * Created: Tuesday, Jan 30, 2018
* 
* * Juneyoung KANG <[email protected]>
* * Gyoha High School
*
* * Creates a today school meal JSON file from the NEIS webpage.
* * Github : https://github.com/Juneyoung-Kang/meal-api/
*
* * How to use?
* http://juneyoung.me/api/school-meal/meal_api.php?countryCode=stu.goe.go.kr&schulCode=J100004922&insttNm=School &schulCrseScCode=4&schMmealScCode=2/
* 
* * For more information, visit github and see README.md
*
* * Licensed under The MIT License
*/

header('Refresh: 60');                        // refresh every 60sec

error_reporting(0);                           // error reporting disable
header("Content-type: text/html; charset=UTF-8");        // // json type and UTF-8 encoding

require "simple_html_dom.php";                // use 'simple_html_dom.php'

$countryCode = $_GET['countryCode'];          // local office of education website
$schulCode =  $_GET['schulCode'];             // school code
$insttNm = $_GET['insttNm'];                  // school name
$schulCrseScCode = $_GET['schulCrseScCode'];  // school levels code
$schMmealScCode = $_GET['schMmealScCode'];    // meal kinds code

// // custom date
// // $schYmd = $_GET['schYmd'];

$MENU_URL = "sts_sci_md01_001.do";            // view weekly table

$today=date("Y.m.d");                         // get date using date() function. ex) 2018.01.01
$day=date("w");                               // get day using date() function. ex) 0==Sunday, 1==Monday, 6==Saturday

// // url for today
$URL="http://" . $countryCode . "/" . $MENU_URL . "?schulCode=" . $schulCode . "&insttNm=" . urlencode( $insttNm ) . "&schulCrseScCode=" . $schulCrseScCode . "&schMmealScCode=" . $schMmealScCode . "&schYmd=" . $today;

// // DOMDocument
$dom=new DOMDocument;

// // load HTML file 
$html=$dom->loadHTMLFile($URL);
$dom->preserveWhiteSpace=false;

// // get elements by tag name
$table=$dom->getElementsByTagName('table');
$tbody=$table->item(0)->getElementsByTagName('tbody');
$rows=$tbody->item(0)->getElementsByTagName('tr');
$cols=$rows->item(1)->getElementsByTagName('td');

// // check blank has values
if($cols->item($day)->nodeValue==null){
    echo '';
}else{
    $final=$cols->item($day)->nodeValue;
}

// // replace unnecessary characters
$final=preg_replace("/[0-9]/", "", $final);
$final=str_replace(".", "", $final);

// // change code number to text
if($schulCrseScCode==1){
    $schulCrseScCode="Kindergarten";
}
if($schulCrseScCode==2){
    $schulCrseScCode="Elementary School";
}
if($schulCrseScCode==3){
    $schulCrseScCode="Middle School";
}
if($schulCrseScCode==4){
    $schulCrseScCode="High School";
}
if($schMmealScCode==1){
    $schMmealScCode="Breakfast";
}
if($schMmealScCode==2){
    $schMmealScCode="Chinese";
}
if($schMmealScCode==3){
    $schMmealScCode="Dinner";
}

// // no meal
if($final==null){
    $final="No school meals today.";
}

// // array
$array[] = array(
    'Education Code' => $countryCode,
    'School Code' => $scholCode,
    'School name' => $instNm,
    'School Type' => $schulCrseScCode,
    'School Meal Type' => $schMmealScCode,
    'Date' => $today,
    'Menu' => $final
);

$fp = fopen('meal.json', 'w');
fwrite($fp, json_encode($array, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
fclose($fp);

?>

meal.json

[
    {
        "교육청 코드": "stu.goe.go.kr",
        "학교 코드": "J100004922",
        "학교 명": ": "êµí•˜ê³ ë“±í•™êµ",
        "학교 종류": "ê³ ë“±í•™êµ",
        "급식 종류": "중식",
        "ë‚ ì§œ": "2018.02.09",
        "메뉴": "오늘은 급식이 없습니다."
    }
]

Can I find a solution?

php encoding json array api

2022-09-21 17:07

1 Answers

Please match the character encoding when writing and reading the meal.json file.

If you search for iconv function and use it, you will get the answer.


2022-09-21 17:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.