6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WikipediaのAPIをPHPから叩いてみる

Posted at

やってみたこと

PHPを使ってWikipediaのAPIを叩いて、
「親の文字体系」という文字列を含むWikipedia日本語版の記事のタイトル一覧を取得した。

参考にした記事

MediaWiki APIを使ってWikipediaの情報を取得

コード

wtest1.php
<html>
<head><title>「親の文字体系」という文字列を含むWikipedia日本語版の記事一覧</title></head>
<body>
<?php
$url='http://ja.wikipedia.org/w/api.php?format=xml&action=query&list=search&srlimit=50&srsearch=';
$url.=urlencode('親の文字体系');

$array_title = array();
$i=0;
while(true){
	//結果が0になるまで50件ずつtitleを取得して配列に入れる
	$xml_string=file_get_contents($url.'&sroffset='.strval($i*50));
	$xml_obj=simplexml_load_string($xml_string);
	if(count($xml_obj->query->search->p)==0){
		$totalhits=$xml_obj->query->searchinfo['totalhits'];
		break;
	}else{
		foreach($xml_obj->query->search->p as $p){
			array_push($array_title,$p['title']);
		}
	}
	$i+=1;
}

//結果出力
print('<p>totalhitsは'.$totalhits.'件</p>');
$num=1;
print('<table border=1>');
foreach($array_title as $title){
	print('<tr>');
	print('<td>');
	print($num);
	print('</td>');
	print('<td>');
	print($title);
	print('</td>');
	print('</tr>');
	$num+=1;
}
print('</table>');
?>
</body>
</html>
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?