//json 인코딩 함수
function json_encode2($data) {
switch (gettype($data)) {
case 'boolean':
return $data?'true':'false';
case 'integer':
case 'double':
return $data;
case 'string':
return '"'.strtr($data, array('\\'=>'\\\\','"'=>'\\"')).'"';
case 'array':
$rel = false; // relative array?
$key = array_keys($data);
foreach ($key as $v) {
if (!is_int($v)) {
$rel = true;
break;
}
}
$arr = array();
foreach ($data as $k=>$v) {
$arr[] = ($rel?'"'.strtr($k, array('\\'=>'\\\\','"'=>'\\"')).'":':'').json_encode2($v);
}
return $rel?'{'.join(',', $arr).'}':'['.join(',', $arr).']';
default:
return '""';
}
}
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
if(array_key_exists('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', $_SERVER)) {
header('Access-Control-Allow-Headers: '
. $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
} else {
header('Access-Control-Allow-Headers: *');
}
if("OPTIONS" == $_SERVER['REQUEST_METHOD']) {
exit(0);
}
//데이터 예시
$result = 쿼리
while($row = mysql_fetch_array($result)){
$result_array[] = $row;
}
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
$result_array=json_encode2($result_array);
print $result_array;
제가 관리하는 사이트 서버가 오래되어서 php 4버전이다 보니
json 함수가 사용이 안되어서 찾은 소스 입니다.
참고 문자 캐릭터형을 정확하게 사용하셔야 합니다.
댓글 2개
게시판 목록
팁게시판
질문은 상단의 QA에서 해주시기 바랍니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 4877 |
|
13년 전 | 2892 | |
| 4876 | 13년 전 | 2368 | ||
| 4875 | 13년 전 | 1967 | ||
| 4874 | 13년 전 | 3762 | ||
| 4873 | 13년 전 | 2072 | ||
| 4872 |
PHPㅡASP프로그래머
|
13년 전 | 1803 | |
| 4871 |
PHPㅡASP프로그래머
|
13년 전 | 1487 | |
| 4870 |
PHPㅡASP프로그래머
|
13년 전 | 1271 | |
| 4869 |
PHPㅡASP프로그래머
|
13년 전 | 1205 | |
| 4868 |
PHPㅡASP프로그래머
|
13년 전 | 4426 | |
| 4867 |
Darby
|
13년 전 | 1602 | |
| 4866 | 13년 전 | 761 | ||
| 4865 | 13년 전 | 837 | ||
| 4864 | 13년 전 | 1504 | ||
| 4863 | 13년 전 | 1409 | ||
| 4862 | 13년 전 | 4251 | ||
| 4861 | 13년 전 | 1036 | ||
| 4860 | 13년 전 | 2684 | ||
| 4859 | 13년 전 | 1250 | ||
| 4858 | 13년 전 | 1627 | ||
| 4857 | 13년 전 | 1268 | ||
| 4856 | 13년 전 | 1914 | ||
| 4855 | 13년 전 | 13172 | ||
| 4854 | 13년 전 | 1182 | ||
| 4853 |
AMDbest
|
13년 전 | 4322 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기