메뉴 건너뛰기

프로그램언어

2019.06.24 19:40

PHP 외부 XML 파싱 하기

조회 수 1942 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
$curl = curl_init();

        $timeout = 5; 

        $url = '가져올 xml  주소';

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);

        $xml = curl_exec($curl);

        curl_close($curl);

        

        $doc = new DOMDocument();

        $doc->preserveWhiteSpace = false;

        $doc->loadXML( $xml); 

 

 

        $xpath = new DOMXpath( $doc);

        // Register the itunes namespace

        $xpath->registerNamespace( 'itunes', 'http://www.itunes.com/dtds/podcast-1.0.dtd');

 

 

              $items = $doc->getElementsByTagName('item');    

                

              foreach( $items as $item) {

            $title = $xpath->query( 'title', $item)->item(0)->nodeValue;

            $type = $xpath->query( 'type', $item)->item(0)->nodeValue;

            $author = $xpath->query( 'itunes:author', $item)->item(0)->nodeValue;

            $update = $xpath->query( 'itunes:subtitle', $item)->item(0)->nodeValue;

            $enclosure = $xpath->query( 'enclosure', $item)->item(0);

            $url = $enclosure->attributes->getNamedItem('url')->value;

            }
정확하게 위의 소스는 iTunes 의  xml 을 가져와서 파싱하는 예제 이다.

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved