d

네이버 환율 파싱

· 2018-10-24 (수) 11:55:24 · 9226 · 4

네이버 환율 파싱 소스를 https://sir.kr/cm_free/1350194 에서 참고하여 작성하여 웹호스팅(카페24 이용 중)에 업로드 하였을 때는 환율정보가 잘 나옵니다...그런데 내부서버(오토셋 10.7.2)로 업로드하면 Warning: Illegal string offset 'USD' in 파일주소 on line 59 등의 에러가 발생합니다.

아마 PHP버젼이 달라서 인 듯합니다. (카페24는 PHP 5.3, 오토셋은 PHP 7.2)

 

이 경우 다음과 같이 변경하여 적용하니 이상없이 잘 작동되네요....

참고하시기 바랍니다. (제 질문에 답변주신 슈와이님께 감사드립니다..)

 

<?

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("/<tbody.*?>.*?<\/[\s]*tbody>/s", $res, $tbody); if(!is_array($tbody)) return 'false';
     preg_match_all('#<tr.*?>(.*?)<\/[\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.*?>(.*?)<\/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();

?>

|

댓글 4개

네이버 올라와서 문득 실시간 검색어 파싱이 생각나네요..ㅜㅜ 예전엔 잘댓는데..지금은 안대서
좋은정보 감사합니다
2018-10-26 (금) 10:50:12
좋은 정보 감사합니다. 덕분에 환율 정보 표시할수 있게 되었습니다.
감사합니다
댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

2,788건
+
제목 글쓴이 날짜 조회
18-10-23 조회 1.3만
18-10-22 조회 1.1만
18-10-21 조회 7,408
18-10-21 조회 6,938
18-10-20 조회 6,857
18-10-18 조회 7,859
18-10-17 조회 8,545
18-10-15 조회 7,063
18-10-14 조회 1.3만
18-10-12 조회 1.7만
18-10-10 조회 7,305
18-10-10 조회 8,349
18-10-10 조회 5,874
18-10-08 조회 6,282
18-10-08 조회 1.1만
18-10-06 조회 9,240
18-10-04 조회 1.4만
18-10-03 조회 7,867
18-10-02 조회 7,597
18-10-02 조회 6,799
18-09-29 조회 6,319
18-09-28 조회 7,790
18-09-21 조회 1.1만
18-09-20 조회 9,731
18-09-17 조회 7,969
18-09-13 조회 8,212
18-09-12 조회 7,015
18-09-05 조회 9,633
18-09-05 조회 7,413
18-09-02 조회 1만