기본 콘텐츠로 건너뛰기

[PHP] 코드이그니터 - RSS읽기

[PHP] 코드이그니터 - RSS읽기

RSS 읽어오기

PHP 프레임워크인 코드이그니터(Codeigniter)에서 RSS정보를 읽어 오는 Controlor 소스입니다.

curl을 사용하여 웹주속의 정보를 읽어오고 xml 데이터를 파싱하여 처리하는 소스를 소개합니다.

예전 블로그의 RSS 내용

Curl, SimpleXmlElement, XML 처리코드

$this->load->helper('html'); $this->load->helper('text'); $feed = array(); $channel_data = array(); $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, "RSS주소"); // USER AGENT 가 없으면 못읽는 사이트가 있다 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); $xmldata = curl_exec($ch); curl_close($ch); // 내용을 못읽은 경우 해결 $xmldata = str_replace("","",$xmldata); $xmldata = str_replace("","",$xmldata); $xml = new SimpleXmlElement($xmldata); if ($xml->channel) { $channel_data['title'] = $xml->channel->title; $channel_data['description'] = $xml->channel->description; foreach ($xml->channel->item as $item) { $data = array(); $data['id'] = (string)$item->id; $data['title'] = (string)$item->title; $data['description'] = (string)$item->contentEncoded; if($data['description'] == "") { $data['description'] = (string)$item->description; } $data['pubDate'] = (string)$item->pubDate; $data['link'] = (string)$item->link; $dc = $item->children('http://purl.org/dc/elements/1.1/'); $data['author'] = (string)$dc->creator; $feed[] = $data; } } else { $channel_data['title'] = $xml->title; $channel_data['description'] = $xml->subtitle; foreach ($xml->entry as $item) { $data = array(); $data['id'] = (string)$item->id; $data['title'] = (string)$item->title; $data['description'] = (string)$item->contentEncoded; if($data['description'] == "") { $data['description'] = (string)$item->description; } $data['pubDate'] = (string)$item->pubDate; $data['link'] = (string)$item->link['href']; $dc = $item->children('http://purl.org/dc/elements/1.1/'); $data['author'] = (string)$dc->creator; $feed[] = $data; } }

View 소스

Controlor 에서 읽은 데이터를 표시하는 코드

예전에 홈페이지 만들때 사용하던 코드 입니다.

from http://skshpapa80.tistory.com/60 by ccl(A) rewrite - 2020-03-07 07:55:37

댓글

이 블로그의 인기 게시물

[PHP] 코드이그니터 - 파일업로드 구현

[PHP] 코드이그니터 - 파일업로드 구현 파일 업로드 이번에 PHP 프레임워크인 코드 이그니터(Codeigniter)를 사용하여 홈페이지를 만드는데 사용한 이미지 업로드용 코드 입니다. upload 라이브러리를 사용하고 app~ 와 같은 위치에 upload 폴더를 만드고 다음 코드를 사용한다음 ajax 로 호출하여 파일을 업로드 합니다. function index() { // Upload 설정 $config['upload_path'] = './upload/'; $config[\'allowed_types\'] = 'gif|jpg|png'; $config['max_size'] = 100; // 100k $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); $data = array(); if (! $this->upload->do_upload("service_image")) { $error = array('error' => $this->upload->display_errors()); } else { //$data = array('upload_data' => $this->upload->data()); $this->output->set_output("./upload/".$this->upload->data('file_name')); } } jquery 를 이용한 파일 업로드 호출 코드 function upload() { var datas, xhr; datas = new FormData(); datas.append( 'service_image', $( ...

이클립스 코드이그나이터 연동 ( eclipse codeigniter )

이클립스 코드이그나이터 연동 ( eclipse codeigniter ) https://ellislab.com/codeigniter/user-guide/installation/downloads.html 위의 사이트에서 코드이그나이터를 다운 받는다. 다운받은 압축 파일을 풀어 준다. 이클립스에서 php 프로젝트를 생성한 공간에 코드이그나이터 압축파일을 복사 붙여넣기 해준다. 위와 같은 화면이 나오면 정상적으로 연동이 된 것 입니다. from http://nahosung.tistory.com/22 by ccl(A) rewrite - 2020-03-06 16:20:55

[CodeIgniter] _rmap을 이용한 화면 상단, 하단 레이어 고정

[CodeIgniter] _rmap을 이용한 화면 상단, 하단 레이어 고정 class Welcome extends CI_Controller { /** * @brief 기본 Wellcome to CodeIgniter! 페이지 지정 */ public function index() { $this->load->view("welcome_message"); } /** * @brief 사이트 헤더, 푸터가 자동으로 추가 */ public function _remap($method) { // brief 헤더 load $this->load->view("layer/headder_view"); if(method_exists($this, $method)) { $this->{"{$method}"}(); } // @brief 푸터 load $this->load->view("layer/footer_view"); } } from http://magic.wickedmiso.com/219 by ccl(A) rewrite - 2020-03-11 02:20:33