<?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>detourist &#187; PHP</title>
	<atom:link href="http://detourist.net/category/web-dev/php/feed" rel="self" type="application/rss+xml" />
	<link>http://detourist.net</link>
	<description>あちこち調整中。不具合はご容赦下さい。</description>
	<lastBuildDate>Tue, 04 May 2010 18:14:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NHKニュースを取得・メール送信するスクリプト</title>
		<link>http://detourist.net/post/get_nhk_news</link>
		<comments>http://detourist.net/post/get_nhk_news#comments</comments>
		<pubDate>Sat, 25 Oct 2008 09:15:20 +0000</pubDate>
		<dc:creator>detourist</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://detourist.net/web-dev/php/get_nhk_news</guid>
		<description><![CDATA[携帯版NHKニュースのヘッドラインおよび記事本文を取得し、新着記事をメールで送信するPHPスクリプト。
テレビは点けないし、新聞は取っていない、Webニュースの巡回すら億劫だという（筆者のような）人間でも、これを携帯に送 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www3.nhk.or.jp/knews/">携帯版NHKニュース</a>のヘッドラインおよび記事本文を取得し、新着記事をメールで送信するPHPスクリプト。</p>
<p>テレビは点けないし、新聞は取っていない、Webニュースの巡回すら億劫だという（筆者のような）人間でも、これを携帯に送るようにしておけば、移動時間や休憩時間を使ってニュースチェックできますよっと。</p>
<p>【注意】 以下のコードでは、不具合によりバックスラッシュが表示されていないので注意。（紺色太文字で表示されている“n”の前に入る。）（スタイルはちゃんと適用されるのに、なんで表示されないかなぁ・・・）</p>
<p>【使用方法】 1.自身で利用可能なサーバマシンにこのスクリプトを設置し、2.キャッシュファイルを作成、3.cronで定期的に実行するように設定。</p>
<div style="position:relative;top:18px;margin-top:-18px;"><small><strong>PHP</strong></small>
</div>
<pre name="code" class="php" style="margin:18px 0;">
&lt;?php
	//変数初期化
	$root_url = &quot;http://www3.nhk.or.jp/knews/&quot;;
	$mailto = &quot;&lt;MAIL ADDRESS&gt;&quot;;
	$output = &quot;&quot;;
	//エンコード設定
	mb_language(&quot;Japanese&quot;);
	mb_internal_encoding(&quot;EUC-JP&quot;);
	mb_detect_order(&quot;ASCII,UTF-8,EUC-JP,JIS,SJIS&quot;);
	//キャッシュファイル指定(※パーミッション注意)
	$f_name = &quot;cache_list.txt&quot;;
	//ファイルの内容を1行ごとに配列に格納
	$cache = file($f_name);
	$cache = array_map(&quot;trim&quot;, $cache); 

	//ニュース一覧を取得
	$data = file_get_contents(&quot;$root_url&quot;);
	$data = mb_convert_encoding($data, &quot;EUC-JP&quot;, &quot;auto&quot;);
	$pattern = &#039;/(t[0-9]+.html)/is&#039;;
	preg_match_all($pattern, $data, $matches);

	//個々の記事を取得
	foreach( $matches[1] as $match ){
		//キャッシュに無ければ、記事を取得
		if(!in_array($match, $cache)){
			$url = $root_url . $match;
			$data = file_get_contents($url);
			$data = mb_convert_encoding($data, &quot;EUC-JP&quot;, &quot;auto&quot;);

			$pattern = &#039;/&lt;body&gt;(.*?)&lt;div/is&#039;;
			preg_match($pattern, $data, $matches);

			$body = $matches[1];
			$body = &quot;◇&quot;. $body;
			$body = preg_replace(&#039;/&lt;br&gt;&lt;br&gt;/&#039;, &quot;n◇&quot;, $body);
			$output .= $body;
			$output .= &quot;nn&quot;;
		}
		$new_cache[] = $match;
	}
	$new_cache = implode(&quot;n&quot;, $new_cache);

	//キャッシュファイル更新
	$fp = fopen($f_name, &quot;w+&quot;);
	fwrite($fp, $new_cache);
	fclose($fp);

	//メール送信
	if($output!=&quot;&quot;){
		$sbject = &quot;NHK News&quot;;
		mb_send_mail($mailto, $sbject, $output, $headers);

		echo(&quot;Mail Sended.n&quot;);
	}else{
		echo(&quot;Null.n&quot;);
	}
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://detourist.net/post/get_nhk_news/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP から POST する方法 その1</title>
		<link>http://detourist.net/post/to_post_by_php</link>
		<comments>http://detourist.net/post/to_post_by_php#comments</comments>
		<pubDate>Sun, 08 Jun 2008 07:17:52 +0000</pubDate>
		<dc:creator>detourist</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://detourist.net/web-dev/php/to_post_by_php</guid>
		<description><![CDATA[PHPを使ってPOSTリクエストを送信する方法としては

fsockopen 関数を使う
sockets ライブラリを使う
stream 関数を使う（※PHP5）
HTTP拡張モジュールを使う
cURL を使う
Zend [...]]]></description>
			<content:encoded><![CDATA[<p>PHPを使ってPOSTリクエストを送信する方法としては
<ul class="modPuki_list1" style="padding-left:16px;margin-left:16px">
<li><a href="http://jp.php.net/manual/ja/function.fsockopen.php" target="_blank">fsockopen 関数</a>を使う</li>
<li><a href="http://jp.php.net/manual/ja/ref.sockets.php" target="_blank">sockets ライブラリ</a>を使う</li>
<li><a href="http://jp.php.net/manual/ja/ref.stream.php" target="_blank">stream 関数</a>を使う（※PHP5）</li>
<li><a href="http://jp.php.net/manual/ja/intro.http.php" target="_blank">HTTP拡張モジュール</a>を使う</li>
<li><a href="http://jp.php.net/manual/ja/function.curl-setopt.php" target="_blank">cURL</a> を使う</li>
<li><a href="http://framework.zend.com/manual/ja/zend.http.html#zend.http.client.parameters" target="_blank">Zend_Http_Client</a> を使う</li>
<li><a href="http://pear.php.net/manual/ja/package.http.http-client.http-client.post.php" target="_blank">PEAR::HTTP_Client</a> を使う</li>
</ul>
<p>（ref. <a href="http://programming-magic.com/?id=134">とても簡単にPHPからPOSTリクエストを送信する方法</a> &#8211; Programming Magic ）</p>
<p>などがあるが、ここでは、環境の変更（ライブラリの追加やバージョンアップなど）が必要でない <a href="http://www.spencernetwork.org/memo/tips-3.php">fsockopen 関数を使う方法</a>を参考にする。</p>
<p>ただし参照元のコードのままでは、POST先からのレスポンスをPHPの出力として（200 OK で）クライアントに渡すため、POST先でエラーだったりリダイレクトされていたりの場合に困ったことになる。そこで、</p>
<p><code>
<pre>
    /* ヘッダ部分とボディ部分を分離 */
    $DATA = split("\r\n\r\n", $response, 2);
</pre>
<p></code></p>
<p>の後ろに、以下のコードを挿入。</p>
<div style="position:relative;top:18px;margin-top:-18px;"><small><strong>PHP</strong></small>
</div>
<pre name="code" class="php" style="margin:18px 0;">
    /* ヘッダを出力 */
    /* これより前に出力が無いよう(空白・空行など)注意！ */
    $headers = explode(&quot;\n&quot;, $DATA[0]);
    foreach( $headers as $header ){
        header($header);
    }
</pre>
<p>これにより、POST先から受け取ったレスポンスをそのままクライアントに渡すことができますよと。</p>
<p>あとはエラー時の対応を付け加えるとなお良さそう。</p>
]]></content:encoded>
			<wfw:commentRss>http://detourist.net/post/to_post_by_php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
