Skip to content

Instantly share code, notes, and snippets.

@siketyan
Last active September 10, 2017 14:41
Show Gist options
  • Save siketyan/624b205af02017dfad7addb2f3404eb0 to your computer and use it in GitHub Desktop.
Save siketyan/624b205af02017dfad7addb2f3404eb0 to your computer and use it in GitHub Desktop.
Wikipediaでキーワード検索して、タイトルと概要部分を返すサンプル
<?php
$keyword = "キーワード";
$json = file_get_contents("https://ja.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&redirects=1&explaintext=1&titles=".urlencode($keyword));
$wiki = json_decode($json, true);
$page = current(array_slice($wiki['query']['pages'], 0, 1, true));
$title = $page['title'];
$body = rtrim(strstr($page['extract'], "==", true), "\n");
// 例
echo "$titleとは...<br>\n$body";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment