//$_POST["cate"]...カテゴリ
//$_POST["sh"]...検索キーワード
if($_POST["cate"] and $_POST["sh"]){
$cate = urlencode($_POST["cate"]);
$sh = urlencode($_POST["sh"]);
$res = simplexml_load_file("https://news.google.com/news/rss/headlines/section/q/$sh/$cate?ned=jp&hl=ja&gl=JP");
rss($res);
}
function rss($obj=NULL){
if(is_object($obj)){
if($obj->channel->item){
$cnt = 0;
foreach ($obj->channel->item as $item) {
$resj[$cnt]["title"] = (string)$item->title;
$resj[$cnt]["link"] = (string)$item->link;
$resj[$cnt]["pubDate"] = (string)$item->pubDate;
$resj[$cnt]["description"] = (string)$item->description;
$resj[$cnt]["source"] = (string)$item->source;
$cnt++;
}
}
}
echo json_encode($resj);
}
google NewsをRSSで取得してjsonで返却するPHPプログラムです。
ご自由にご使用ください。