我正在使用以下代码从维基百科api获取图片,但目前它在该关键字上给我的图像是随机的,比如如果我搜索“西班牙”,它会给我带有单词西班牙的随机图像,但我需要像我们在维基百科中得到的西班牙地方的图像。有人能帮我吗?
<form action="" method="get">
<input type="text" name="search">
<input type="submit" value="Search">
</form>
<?php
if(@$_GET['search']){
function get_wiki_image( $search, $limit) {
$streamContext = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
),
);
$url = 'https://en.wikipedia.org/w/';
$url .= '/api.php?action=query&format=json&list=allimages&aifrom=' . $search . '&ailimit=' . $limit;
$context = stream_context_create($streamContext);
if(FALSE === ($content = @file_get_contents($url, false,$context)) ) {
return false;
} else {
$data = json_decode($content,true);
$ret = array();
foreach($data['query']['allimages'] as $img) {
$ret[] = $img['url'];
}
return $ret;
}
}
$search = ucwords($_GET['search']);
$images = get_wiki_image($search,500);
foreach($images as $img) {
echo "<img src='{$img}' height='50' width='50'>";
}
}
?>发布于 2020-11-19 04:49:53
您可以使用PageImages API来实现此目的。通常,它会返回文章中的第一张图片,但是,根据维基百科的配置,在某些情况下,它可能会返回不同的图片。
例如,要获取“巴塞罗那”文章的图像,请调用https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&titles=Barcelona&piprop=original。
如果您需要某个大小的图片,也可以调用https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&titles=Barcelona&pithumbsize=250。
https://stackoverflow.com/questions/64894218
复制相似问题