Last active
September 10, 2017 14:41
-
-
Save siketyan/624b205af02017dfad7addb2f3404eb0 to your computer and use it in GitHub Desktop.
Wikipediaでキーワード検索して、タイトルと概要部分を返すサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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