Copy codeThe Code is as follows:
$ Xmlfile = 'news/'. date ('ymmdh').'. xml ';
$ Sourcexml = 'HTTP: // www.chinanews.com.cn/rss/scroll-news.xml ';
If (! File_exists ($ xmlfile )){
$ Str = @ file ($ sourcexml) or die ('An error occurred while loading the file. ');
$ Fp = fopen ($ xmlfile, 'w') or die ('write cache failed! ');
Fputs ($ fp, $ str );
Fclose ($ fp );
Then read and output HTML with simplexml
Copy codeThe Code is as follows:
$ Xml = simplexml_load_file ($ xmlfile );
$ C = $ xml-> channel;
$ K = 0;
Foreach ($ c-> item as $ v ){
$ K ++;
Echo"
<Div class = \ "newsline \"> · <a title = \ "$ v-> title \" href = \ "$ v-> link \" target = \"_ blank \ "> $ v-> title </a> </div>
";
Later I encountered a problem, that is, RSS often contains GBK traditional Chinese characters. Google has published many articles on Character Set conversion. A solution is obtained:
Copy codeThe Code is as follows:
$ Str = mb_convert_encoding (join ('', $ str)," gb2312 "," GBK ");
This method cannot be simplified in Traditional Chinese, but it will be converted into a garbled code, but at least simplexml_load_file ($ xmlfile) can be successfully executed. Because these traditional Chinese characters usually appear in the description
.
Then I encountered another problem, as shown in the description: "xinnet, September December 30 (Wen Yu e ?" Simplexml_load_file failed.
So far, we have to use a regular expression to obtain the title and link. (In some articles, regular expressions may be more efficient and will be verified later)
The Code is as follows:
Copy codeThe Code is as follows:
$ Fp = fopen ($ xmlfile, 'R ');
If ($ fp)
{
$ Data = fread ($ fp, filesize ($ xmlfile ));
Fclose ($ fp );
Preg_match_all (
"/<Item> <title> (.*?) <\/Title> <link> (.*?) <\/Link>/I ", $ data, $ out, PREG_SET_ORDER );
Foreach ($ out as $ key => $ v)
{
$ V [1] = iconv ('gbk', 'utf-8', $ v [1]);
Echo"
<Div class = \ "newsline \"> · <a title = \ "$ v [1] \" href = \ "$ v [2] \" target = \"_ blank \ "> $ v [1] </a> </div>
";
}
}