时间:2021-07-01 10:21:17 帮助过:21人阅读
'; $xmlOutput = $xmlPre . $xmlItemContent . $xmlAppend ; echo $xmlOutput; /** * @param $url * @return array itemObj */ function parseItems( $url ) { $xmlObj = simplexml_load_file( $url ); $items = $xmlObj -> channel -> item; $resultArr = array(); foreach( $items as $item ): $resultArr []= makeRssItemObj( $item ); endforeach; return $resultArr; } /** * @param $item * @return itemObj */ function makeRssItemObj( $item ) { // $item->link = "demo.html"; $urlContent = fileGetContent( $item->link ); //如果不支持curl 就用file_get_content 或者socket 连接来实现. $urlContent = @iconv("gb2312","utf-8",$urlContent); // $content = iconv("gb2312" , "utf-8" , $content); preg_match("/id=\"newscontent\">([\d\D]+)/iU" , $urlContent , $matchArr); $item->description = $matchArr[1]; return $item; } /** * @param $itemObjArr * @return string */ function makeRssItemString( $itemObjArr ) { $result = ""; foreach( $itemObjArr as $itemObj ): $result .= " http://sz.focus.cn/newscenter/xwsy.html http://images.house.focus.cn/img/newhouselogo.gif 热点新闻-搜狐焦点网深圳站 http://sz.focus.cn/newscenter/xwsy.htmlCopyright 2012, sohu.com Inc., all rights reserved zh-cn Mon, 02 Jul 2012 21:44:21 +0800 Mon, 02 Jul 2012 21:44:21 +0800 地产综合 30 '; $xmlAppend = '- "; $result .= "
title ]]> "; $result .= "$itemObj->link"; $result .= "description ]]> "; $result .= "mu_rain"; $result .= "$itemObj->pubDate "; $result .= "- "; endforeach; return $result; } // ------------------------------------ /** * regulary show the string or object or json. * 规格化显示 * * @param $str 对象的实例 * @package P * @subpackage String * @category Putils * @author mu_rain * @return mixed */ // ------------------------------------ function pr($array,$title = 'DEBUG',$type = 'array' , $width = '') { $title .= date("Y-m-d H:i:s"); $widthStr = ""; if( $width) $widthStr = "width:$width"."px"; echo ""; } // ------------------------------------ /** * * get the urlContent use curl * @package KDG * @subpackage common * @category mu_rain * @author 徐兴 */ // ------------------------------------ function fileGetContent($url) { $ch = curl_init(); $timeout = 30; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $contents = trim(curl_exec($ch)); curl_close($ch); return $contents; }