# google NewsをRSSで取得してjsonで返却するPHPプログラム
```
<?php
//$_POST["sh"]...検索キーワード
if ($_POST["sh"]) {
$sh = urlencode(@xss_defence($_POST["sh"]));
$res = simplexml_load_file("https://news.google.com/news/rss/headlines/section/q/$sh/?ned=jp&hl=ja&gl=JP");
rss($res);
}
function rss(object $obj = null):void
{
if (isset($obj->channel->item)) {
if ($obj->channel->item) {
$cnt = 0;
foreach ($obj->channel->item as $item) {
$result[$cnt]["title"] = (string)$item->title;
$result[$cnt]["link"] = (string)$item->link;
$result[$cnt]["pubDate"] = (string)$item->pubDate;
$result[$cnt]["description"] = (string)$item->description;
$result[$cnt]["source"] = (string)$item->source;
$cnt++;
}
}
}
echo json_encode($result);
}
function xss_defence(mixed $val):mixed
{
if(!isset($val))return false;
if(is_array($val)){
foreach ($val as $key => $value) {
# code...
$val[$key] = strip_tags($value);
$val[$key] = htmlentities($val[$key],ENT_QUOTES);
}
}else{
$val = strip_tags($val);
$val = htmlentities($val,ENT_QUOTES);
}
return $val;
}
```
google NewsをRSSで取得してjsonで返却するPHPプログラムです。
ご自由にご使用ください。
[  Googleログインに変更した話.
](https://zip358.com/2026/01/28/google%e3%83%ad%e3%82%b0%e3%82%a4%e3%83%b3%e3%81%ab%e5%a4%89%e6%9b%b4%e3%81%97%e3%81%9f%e8%a9%b1.html)
[  広告と収入と狭間.
](https://zip358.com/2026/01/27/%e5%ba%83%e5%91%8a%e3%81%a8%e5%8f%8e%e5%85%a5%e3%81%a8%e7%8b%ad%e9%96%93.html)
[  年齢とか技術とか見た目とかさ.
](https://zip358.com/2026/01/26/%e5%b9%b4%e9%bd%a2%e3%81%a8%e3%81%8b%e6%8a%80%e8%a1%93%e3%81%a8%e3%81%8b%e8%a6%8b%e3%81%9f%e7%9b%ae%e3%81%a8%e3%81%8b%e3%81%95.html)
[  OSS:オープンソースソフトウェア
](https://zip358.com/2026/01/25/oss%ef%bc%9a%e3%82%aa%e3%83%bc%e3%83%97%e3%83%b3%e3%82%bd%e3%83%bc%e3%82%b9%e3%82%bd%e3%83%95%e3%83%88%e3%82%a6%e3%82%a7%e3%82%a2.html)
[  お笑い芸人は貴方より
](https://zip358.com/2026/01/24/%e3%81%8a%e7%ac%91%e3%81%84%e8%8a%b8%e4%ba%ba%e3%81%af%e8%b2%b4%e6%96%b9%e3%82%88%e3%82%8a.html)