api.php?action=query&titles=India&prop=images
为您提供按字母顺序排序的所有图像的完整列表。您可以从非api页面上的文档顺序中检索第一张图像。可能如果你将两者结合起来,你会得到最大的好处:
$topic = 'India';
$url = sprintf('http://en.wikipedia.org/wiki/%s', urlencode($topic));
$options = array(
'http' => array(
'user_agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:50',
)
);
$context = stream_context_create($options);
libxml_set_streams_context($context);
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xp = new DOMXPath($doc);
$result = $xp->query('(//img[@class = "thumbimage"])[1]');
$image = ($result && $result->length) ? $result->item(0) : NULL;
echo $doc->saveXML($image), "\n";