메인>>유머

네이버 환율 파싱

2022-01-25 16:55:00 3163

function Ncurrency() { 
 # 데이터 호출
     $url = 'https://finance.naver.com/marketindex/exchangeList.nhn';
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POST, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     $response = curl_exec($ch);
     curl_close($ch);
     $res = iconv('euc-kr', 'UTF-8', $response); if(!$response) return 'false';
 
     # 파싱
     preg_match("/.*?<\/[\s]*tbody>/s", $res, $tbody); if(!is_array($tbody)) return 'false';
     preg_match_all('#(.*?)<\/[\s]*tr>#s', $tbody[0], $tr); if(!is_array($tr)) return 'false';
 
     $Data = array();
     foreach($tr[0] as $k=>$v) {
 
         unset($td, $akey);
         preg_match_all('#(.*?)<\/td>#s', $v, $td);
         $td = $td[0];
         $akey = preg_replace('/([\xEA-\xED][\x80-\xBF]{2})+/', '', strip_tags($td[0]));
         $akey = trim(str_replace('JPY (100)', 'JPY', $akey));
         $akey = trim(str_replace('100', '', $akey)); if(!$akey) return 'false';
         $Data[$akey]['통화명'] = trim(strip_tags($td[0]));
         $Data[$akey]['매매기준율'] = str_replace(',', '', trim(strip_tags($td[1])));
         $Data[$akey]['현찰살때'] = str_replace(',', '', trim(strip_tags($td[2])));
         $Data[$akey]['현찰팔때'] = str_replace(',', '', trim(strip_tags($td[3])));
         $Data[$akey]['송금보낼때'] = str_replace(',', '', trim(strip_tags($td[4])));
         $Data[$akey]['송금받을때'] = str_replace(',', '', trim(strip_tags($td[5])));
         $Data[$akey]['미화환산율'] = str_replace(',', '', trim(strip_tags($td[6])));
     }
 
     return $Data;
 }
 $Data = Ncurrency();