Snoopy.class 관련 질문
본문
위에 팁 보면서 하고 있는데
$newpost = array(
'mb_id' => 'phoneclub',
'bo_table' => $bo_table,
'wr_subject' => $item->title,
'wr_content' => $item->description."...<br><br>... [<a target='_blank' href='".$item->link."'>더보기</a>]",
'wr_link1' => $item->link
);
insert_write($newpost);
$item->title 타이틀은 잘 가지고 오는데
<ht:news_item_title> 이런류의 앞에 ht가 붙은 건 못가져 오더라구요..
$item->ht:news_item_title; 이렇게 해봐도 안되고 팁좀 주실수 있나요?
답변 1
<ht:news_item_title> 이런 류의 내용은 2차원 배열의 데이타로 저장되기 때문에
2차원 키를 이용하여 값을 가져와야 합니다.
예를 들어 설명하자면,
먼저 파싱할 xml 데이타가 다음과 같다고 가정하고
$xml_content = '<?xml version="1.0" encoding="UTF-8"?>
<rss>
<channel>
<item>
<title>그녀는 예뻤다</title>
<description>그녀는 예뻤다 1회</description>
<link>https://www.google.com/trends/hottrends?pn=p23#a=20150917-%EA%B7%B8%EB%85%80%EB%8A%94+%EC%98%88%EB%BB%A4%EB%8B%A4</link>
<pubDate>Thu, 17 Sep 2015 01:00:00 +0900</pubDate>
<ht:picture>//t0.gstatic.com/images?q=tbn:ANd9GcRaIsjysULSRFGj6FSMYiCzXd77LOuMKm9_T18a93Rn5sGt4mzcrJlLS9vT06JmHZybH7KOy0RQ</ht:picture>
<ht:picture_source>오마이뉴스</ht:picture_source>
<ht:approx_traffic>5,000+</ht:approx_traffic>
<ht:news_item>
<ht:news_item_title><<b>그녀는 예뻤다</b>></ht:news_item_title>
<ht:news_item_snippet><<b>그녀는 예뻤다</b>>는 제목처럼 과거형으로 '예뻤던'</ht:news_item_snippet>
<ht:news_item_url>http://www.ohmynews.com/NWS_Web/View/at_pg.aspx?CNTN_CD=A0002144814</ht:news_item_url>
<ht:news_item_source>오마이뉴스</ht:news_item_source>
</ht:news_item>
</item>
</channel>
</rss>';
위의 소스에서 <item> ~ </item> 영역 안의 1단계 엘리먼트(title, ht:picture 등)는 1차원 배열이 되고
2단계 엘리먼트는 2차원 배열이 되게 됩니다.
여기서 2단계 엘리먼트는 <ht:news_item> ~ </ht:news_item> 영역 안에 있는 <ht:news_item_title>, <ht:news_item_snippet> 등 입니다.
따라서, <ht:news_item_title>를 출력하려면 위의 팁 링크에 게시된 post_rss.php 파일의
foreach 루푸 안에서 다음과 같이 하면 됩니다.
$xml = @simplexml_load_string($snoopy->results);
foreach($xml->channel->item as $item) {
//1차원 배열 <ht:*> 부분 가져오기 추가
$picture = '<img src="'.$item->picture.'" alt="'.$item->picture_source.'">';
//2차원 배열 <ht:news_item> 영역 내용 가져오기 추가
foreach($item->news_item as $news){
$news_item = $news->news_item_title.'<br> ... (<a href="'.$news->news_item_url.'" target="_blank">더보기</a>)';
}
//위의 추가로 불러온 내용을 아래의 wr_content 값에 추가함
$sql = "select count(*) as cnt from $write_table where wr_link1 = '".$item->link."' and wr_is_comment = 0";
$row = sql_fetch($sql);
if ($row['cnt'] == 0){
$newpost = array(
'mb_id' => 'admin',
'bo_table' => $bo_table,
'wr_subject' => $item->title,
'wr_content' => $item->description.'...<br><br>... [<a href="'.$item->link.'" target="_blank">더보기</a>]'.$news_item.'<br>'.$picture,
'wr_link1' => $item->link
);
insert_write($newpost);
}
}