PHPでGoogleニュースの見出しを表示(Windowsアプリ版)

(1/1)
PHPでGoogleニュースの見出しを表示」では、RSSを使ってGoogleニュースの見出しを取得するプログラムをつくったが、これをWindowsアプリに移植する。
検索結果をダブルクリックすると、標準ブラウザを使って当該記事を表示することができる。

(2020年8月15日)Googleニュース検索URLの変更に対応した。

サンプル・プログラム

PHPでWindowsGoogleニュースの見出しを表示
圧縮ファイルの内容
googlenewswin.exeサンプル・プログラム
googlenewswin.bcpコンパイル用ファイル
googlenewswin/googlenewswin.phpwソース・プログラム
googlenewswin/googlenewswin.rcリソース
googlenewswin/PEAR.phpPEAR.php
googlenewswin/*.dll実行時に必要になるDLL
googlenewswin/icon/*アイコン
googlenewswin/include/*winbinder用インクルード・ファイル
googlenewswin/XML/*XML Unserializer

解説:RSS情報の取得

0251:     $query = wb_get_text(wb_get_control($windowIDC_QUERY_EDIT));
0252:     if ($query != '') {
0253:         $query = sjis_internal($query);
0254:         $url = getURL_GoogleNewsSearch($query$nums);
0255: 
0256:         $options = array('parseAttributes' => TRUE);
0257:         $xml = new XML_Unserializer($options);
0258:         $xml_data = @file_get_contents_ssl($url);
0259:         $xml->unserialize($xml_dataFALSE);
0260:         $rss = $xml->getUnserializedData();
0261: 
0262:         if (! isset($rss['channel'])) {
0263:             $res = '検索に失敗しました.';
0264:         } else {
0265:             $i = analyseGoogleNewsWin($rss$items);
0266:             usort($items, "sortItems");
0267:         }
0268:     }

PHPでGoogleニュースの見出しを表示」では、検索結果として返ってくるRSSオブジェクトを、Magpie RSSを使って解析した。
ここでは代わりに、「PHPで Windows用週間天気予報プログラムを作る」で紹介したのと同様、XML_Unserializer で処理することにする。

0153: /**
0154:  * httpsサイトからコンテンツを受信
0155:  * @param string $url "https://" から始まるURL
0156:  * @return string取得したコンテンツ/FALSE取得エラー
0157: */
0158: function file_get_contents_ssl($url) {
0159:     $ch = curl_init($url);
0160:     curl_setopt($chCURLOPT_HEADERFALSE);
0161:     curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
0162:     curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE); //サーバ証明書検証をスキップ
0163:     curl_setopt($chCURLOPT_SSL_VERIFYHOSTFALSE); //  〃
0164:     $result = curl_exec($ch);
0165:     curl_close($ch);
0166: 
0167:     return $result;
0168: }

検索サイトは https:blue] に変更になったため、本処理系に内蔵されている  file_get_contents  関数では読み込めない。そこで、「PHPで今日は何の日か調べる(Windowsアプリ版)」で作った file_get_contents_ssl を利用する。

解説:検索結果の解析

0197: /**
0198:  * Googleニュースを検索し、解析結果を配列に格納(Windowsアプリ版)
0199:  * @param object $rss   MagpieRSSの出力
0200:  * @param array  $items 解析結果を格納する配列
0201:  * @return int 解析結果
0202: */
0203: function analyseGoogleNewsWin($rss, &$items) {
0204:     static $format = 'Y/m/d(J) H:i:s';       //日時フォーマット
0205:     static $pat = "(.+) \- ([^\-]+)";     //タイトルの書式
0206: 
0207:     $key = 0;
0208:     foreach ($rss['channel']['item'] as $val) {
0209:         //タイトルと掲載紙を分離
0210:         if (mb_ereg($pat$val['title'], $arr) != FALSE) {
0211:             $items[$key][0] = internal_sjis($arr[1]);
0212:             $items[$key][1] = internal_sjis($arr[2]);
0213:         } else {
0214:             $items[$key][0] = internal_sjis($val['title']);
0215:             $items[$key][1] = '';
0216:         }
0217:         $items[$key][2] = internal_sjis(jdate($formatstrtotime($val['pubDate'])));
0218:         $items[$key][3] = strtotime($val['pubDate']);   //UNIX時間
0219:         $items[$key][4] = $val['link'];
0220:         $key++;
0221:     }
0222: 
0223:     return $key;
0224: }

検索結果の解析では、再三述べているように、bamcompile でEXEプログラムをつくる場合、preg系関数が正常に働かないため、 mb_ereg  系関数で代替している。

コンパイル

コマンドラインから "bamcompile googlenewswin.bcp" を実行する。コンパイルが完了すると、"nengowin.exe" が生成される。"googlenewswin.exe" は、DLL不要で、単独で動作するEXEプログラムである。

参考サイト

(この項おわり)
header