第3回 勉強会 特設サイト | h9 Inc.

PHP < 基本操作 RSSリーダー等 >

→ SimpleXML(PHP5)
→ mod_rewrite
→ ディレクトリと画像

■SimpleXML(PHP5)



PHP のバージョン 5 にバンドルされた SimpleXML


サンプル 01

<?php header("Content-type:text/html;charset=utf-8"); $xml = simplexml_load_file('http://www.goldwin.co.jp/scott/snow/cgi/?feed=rss2'); echo "<pre>"; var_dump($xml); echo "</pre>"; ?>
※var_dump(変数); 変数の構造化した情報を参照


サンプル 02

<?php function convert($str) { return mb_convert_encoding($str,"SJIS","UTF-8"); } $article = simplexml_load_file('http://www.goldwin.co.jp/scott/snow/cgi/?feed=rss2'); echo convert($article->channel->item[0]->title); print "<br>\n"; echo convert($article->channel->item[0]->description); print "<br>\n"; echo convert($article->channel->item[0]->link); print "<br>\n"; $ddd=$article->channel->item[0]->pubDate; $date = strtotime($ddd); $henkan = mb_convert_encoding('Y年m月d日','UTF-8','SJIS'); $s_date = mb_convert_encoding(date($henkan,$date), 'SJIS', 'UTF-8'); print $s_date; ?>

//////////////////////////////////////////////////////////////
取得するRSSの中身

<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" > <channel> <title>SCOTT WINTERSPORT NEWS</title> <atom:link href="http://www.goldwin.co.jp/scott/snow/cgi/?feed=rss2" rel="self" type="application/rss+xml" /> <link>http://www.goldwin.co.jp/scott/snow/cgi</link> <description>SCOTT WINTERSPORT NEWS</description> <lastBuildDate>Wed, 01 Jul 2009 04:56:28 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.8</generator> <language>ja</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item> <title>09-10 SCOTT SNOWウェブサイトオープン</title> <link>http://www.goldwin.co.jp/scott/snow/cgi/?p=73</link> <comments>http://www.goldwin.co.jp/scott/snow/cgi/?p=73#comments</comments> <pubDate>Wed, 01 Jul 2009 04:56:28 +0000</pubDate> <dc:creator>castmc</dc:creator> <category><![CDATA[最新情報]]></category> <guid isPermaLink="false">http://www.goldwin.co.jp/scott/snow/cgi/?p=73</guid> <description><![CDATA[2009-2010のSCOTT SNOWウェブサイトがリニューアルオープンしました。 今季もアルペンスキー、デモスキー、フリーライドスキー、スノーボードと幅広いスノーシーンをサポートします。 是非ご覧ください! ]]></description> <content:encoded><![CDATA[<p>2009-2010のSCOTT SNOWウェブサイトがリニューアルオープンしました。<br /> 今季もアルペンスキー、デモスキー、フリーライドスキー、スノーボードと幅広いスノーシーンをサポートします。<br /> 是非ご覧ください!</p> ]]></content:encoded> <wfw:commentRss>http://www.goldwin.co.jp/scott/snow/cgi/?feed=rss2&amp;p=73</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>


■mod_rewrite



※ヘテムル仕様

/////////////////////////////////////////////////////
拡張子書き換え ※phpとしては動作しない。

ReAddHandler php5-script .php
RewriteEngine on
RewriteRule ^(.*)\.php$ /mod/$1.html [R=301,L]


/////////////////////////////////////////////////////
phpとして動作

AddHandler php5-script .php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z_\-]+)\.html$ $1\.php [L]


☆mod_rewrite活用法

phpのincludeを利用して、各部をファイルに分散します。

構成例としては

・inc.php まとめるファイル
・head.php ヘッダ部
・body.txt ボディ部
・foot.php フッタ部

のようにします。

.phpファイルと.txtファイルでは、
記述方法が異なります。

それぞれは以下に。

inc.php まとめるファイル
<?php include "head.php"; //括弧なし include('body.txt'); //括弧付き $foot="foot.php"; //変数 include $foot; ?>

head.php ヘッダ部
<?php print <<<TT <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <head> <title>h9 Inc.</title> </head> TT; ?>

body.txt ボディ部
<body> 呼んだ? </body>

foot.php フッタ部
<?php print <<<FF </html> FF; ?>


■ディレクトリと画像



h9viewer.php

使用方法

1・h9viewer.phpと同じ階層に、画像を入れたフォルダをアップ
2・フォルダ名は、必ず半角英数文字 ※クエリ参照の為
3・h9viewer.php?フォルダ名でページスイッチ付きのビューアが完成。


中身

<?php // $query=$_SERVER['QUERY_STRING']; //echo $query; if(!$query) exit('ディレクトリ名を指定してください'); if(!strstr("=",$query)){ list($dataDir,$no)=explode("=",$query); } $imgnum = $no; $image = $no; // $imagedir = @opendir($dataDir) or die ("ディレクトリ名が不正です"); while (false !== ($file[] = readdir($imagedir))); closedir($imagedir); natsort($file); if (!$imgnum) { $imgnum = 0; $image = 0; } // $imagecount = 0; while (false !== ($jpg = each($file))){ if (ereg(".jpg$", $jpg[1])) { $imagecount++; if ($imgnum == ($image+1)) { break; } if ($imagecount > $image) { print "<img src='".$dataDir."/".$jpg[1]."'>\n"; //print "<br>".$jpg[1]."<hr>\n"; $imgnum++; } } } // print "<br>\n"; // if ($imgnum > 1) { $backno = $imgnum-2; print "<a href='?".$dataDir."=".$backno."'>back</a> | \n"; }else{ print "back | \n"; } if ($imgnum < $imagecount) { print "<a href='?".$dataDir."=".$imgnum."'>next</a>\n"; }else{ print "next\n"; } ?>



☆☆☆おまけ

動的アドレスと静的アドレスに変換

・htaccessを設置

AddHandler php5-script .php RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/(.*)\.html$ h9viewer.php?$1=$2 [L]

・下記の部分を書き換え

print "<img src='http://www.highqualityandliteracy.net/study/php/v/".$dataDir."/".$jpg[1]."'>\n";

ディレクトリが一階層上がる為、imgタグ内のアドレスをフルパスにする必要があります。

※ページスイッチは、別に作成する必要があります。


↑この辺りがブログシステムに共通する静的アドレスの書き換え箇所になります。

h9 Inc.