//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개
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 330 |
prosper
|
20년 전 | 2287 | |
| 329 |
prosper
|
20년 전 | 1917 | |
| 328 |
prosper
|
20년 전 | 1711 | |
| 327 | 20년 전 | 3776 | ||
| 326 | 20년 전 | 4865 | ||
| 325 |
hwatta
|
20년 전 | 2493 | |
| 324 |
|
20년 전 | 3150 | |
| 323 | 20년 전 | 5875 | ||
| 322 |
hwatta
|
20년 전 | 2856 | |
| 321 |
hwatta
|
20년 전 | 2329 | |
| 320 |
yesmoa
|
20년 전 | 4581 | |
| 319 | 20년 전 | 2694 | ||
| 318 | 20년 전 | 2244 | ||
| 317 |
kyodon
|
20년 전 | 2765 | |
| 316 | 20년 전 | 2587 | ||
| 315 |
|
20년 전 | 2890 | |
| 314 |
|
20년 전 | 3347 | |
| 313 |
|
20년 전 | 2653 | |
| 312 |
yesmoa
|
20년 전 | 4730 | |
| 311 | 20년 전 | 3286 | ||
| 310 |
홀로남은자
|
20년 전 | 4574 | |
| 309 | 20년 전 | 3011 | ||
| 308 | 20년 전 | 4144 | ||
| 307 | 20년 전 | 4380 | ||
| 306 | 20년 전 | 7042 | ||
| 305 | 20년 전 | 3877 | ||
| 304 | 20년 전 | 2776 | ||
| 303 |
크리스탈처럼
|
20년 전 | 4414 | |
| 302 | 20년 전 | 2215 | ||
| 301 |
|
20년 전 | 4332 | |
| 300 | 20년 전 | 3819 | ||
| 299 | 20년 전 | 2635 | ||
| 298 | 20년 전 | 4845 | ||
| 297 |
|
20년 전 | 2535 | |
| 296 | 20년 전 | 4540 | ||
| 295 | 20년 전 | 3573 | ||
| 294 | 20년 전 | 3596 | ||
| 293 | 20년 전 | 3824 | ||
| 292 | 20년 전 | 3220 | ||
| 291 |
yesmoa
|
20년 전 | 5960 | |
| 290 | 20년 전 | 2947 | ||
| 289 | 20년 전 | 5870 | ||
| 288 |
|
20년 전 | 2385 | |
| 287 |
|
20년 전 | 1818 | |
| 286 |
|
20년 전 | 2157 | |
| 285 |
|
20년 전 | 3545 | |
| 284 |
|
20년 전 | 2042 | |
| 283 |
|
20년 전 | 4406 | |
| 282 | 20년 전 | 3385 | ||
| 281 |
|
20년 전 | 2218 | |
| 280 |
|
20년 전 | 7817 | |
| 279 | 20년 전 | 5567 | ||
| 278 | 20년 전 | 3003 | ||
| 277 |
|
20년 전 | 5577 | |
| 276 | 20년 전 | 2367 | ||
| 275 | 20년 전 | 2623 | ||
| 274 | 20년 전 | 2403 | ||
| 273 | 20년 전 | 2237 | ||
| 272 | 20년 전 | 2163 | ||
| 271 | 20년 전 | 2625 | ||
| 270 | 20년 전 | 2675 | ||
| 269 | 20년 전 | 2515 | ||
| 268 | 20년 전 | 2700 | ||
| 267 | 20년 전 | 2382 | ||
| 266 | 20년 전 | 2575 | ||
| 265 | 20년 전 | 3517 | ||
| 264 |
|
20년 전 | 5378 | |
| 263 |
|
20년 전 | 3742 | |
| 262 | 20년 전 | 3202 | ||
| 261 |
허저비
|
20년 전 | 5947 | |
| 260 |
|
20년 전 | 5724 | |
| 259 | 20년 전 | 4148 | ||
| 258 | 20년 전 | 2384 | ||
| 257 | 20년 전 | 3205 | ||
| 256 | 20년 전 | 1918 | ||
| 255 | 20년 전 | 1587 | ||
| 254 | 20년 전 | 3158 | ||
| 253 | 20년 전 | 3551 | ||
| 252 | 20년 전 | 5134 | ||
| 251 | 20년 전 | 5827 | ||
| 250 | 20년 전 | 3687 | ||
| 249 | 20년 전 | 5033 | ||
| 248 | 20년 전 | 3302 | ||
| 247 | 20년 전 | 3658 | ||
| 246 |
|
20년 전 | 7966 | |
| 245 |
|
20년 전 | 5928 | |
| 244 | 20년 전 | 4498 | ||
| 243 |
|
20년 전 | 4077 | |
| 242 | 20년 전 | 2808 | ||
| 241 | 20년 전 | 2754 | ||
| 240 | 20년 전 | 2390 | ||
| 239 | 20년 전 | 1679 | ||
| 238 |
아우겐나이스
|
20년 전 | 2285 | |
| 237 |
email
|
20년 전 | 3698 | |
| 236 | 20년 전 | 4182 | ||
| 235 | 20년 전 | 10475 | ||
| 234 | 20년 전 | 5083 | ||
| 233 | 20년 전 | 3386 | ||
| 232 | 20년 전 | 3228 | ||
| 231 | 20년 전 | 3870 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기