-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php.bak
More file actions
54 lines (44 loc) · 1.4 KB
/
Copy pathtest.php.bak
File metadata and controls
54 lines (44 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
function sendGetRequest($startdate,$code,$num) {
// cURL 세션 초기화
$url="https://apis.data.go.kr/1160100/service/GetSecuritiesProductInfoService/getETFPriceInfo?";
$url.="serviceKey=OAs5IRcjVD8bOJlCLWyg9WgN2AohcrPd4x8bZzPxp9XRituQpMTVzGCSyDSTVYmi19NSYmmrpwfuo1bN5G3uSA%3D%3D&";
$url.="numOfRows=".$num."&pageNo=1&resultType=json&";
$url.="beginBasDt=".$startdate."&likeSrtnCd=".$code;
$ch = curl_init();
// 옵션 설정
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
// 요청 실행 및 응답 저장
$response = curl_exec($ch);
// 에러 처리
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
// cURL 세션 닫기
curl_close($ch);
return $response;
}
// 사용 예시
$st="20240304";
$code="114100";
$num="90";
$json_string = sendGetRequest($st,$code,$num);
$data = json_decode($json_string, true);
if ($data['response']['body']['totalCount'] > 0){
$items=$data['response']['body']['items']['item'];
$needkeys=array('basDt','srtnCd', 'itmsNm', 'clpr');
$stackarray=array();
foreach ($items as $item => $value){
foreach($needkeys as $key => $needkey){
$stackarray[$needkey][$item]=$value[$needkey];
}
}
}
foreach($needkeys as $key => $needkey){
$stackarray[$needkey][$item]=$value[$needkey];
krsort($stackarray[$needkey]);
}
print_r($stackarray);
?>