我不知道导出API的对象是谁
因此,URL是:
我得到了
{"batchcomplete":"","query":{"pages":{"89265": {"pageid":89265,"ns":0,"title":"Jean-Claude Van Damme","thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Jean-Claude_Van_Damme_2012.jpg/141px-Jean-Claude_Van_Damme_2012.jpg","width":141,"height":200},"pageimage":"Jean-Claude_Van_Damme_2012.jpg"}}}}
谁只能导出源码?
现在我有了下面的代码:
$json=file_get_contents("https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=200&titles=$title");
$details=json_decode($json);
echo $details['thumbnail']['source'];
那么我是做谁的呢?
发布于 2016-01-08 23:30:56
您可以使用json_decode并将true
作为第二个参数传递。
从文档中:
如果为TRUE,则返回的对象将转换为关联数组。
那么我想你要找的是:
$json=file_get_contents("https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=200&titles=Jean-Claude%20Van%20Damme");
$details=json_decode($json, true);
$source = $details['query']['pages']['89265']['thumbnail']['source'];
echo $source;
将导致:
发布于 2016-01-08 23:46:51
尝尝这个。
$json = file_get_contents("https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=200&titles=Jean-Claude%20Van%20Damme");
$details = json_decode($json);
$pages = $details->query->pages;
$output = '';
foreach ($pages as $pageid => $content){
$image_url = $content->thumbnail->source;
$title = $content->title;
$output .= '<img src="'.$image_url.'"/>'.$title;
}
echo $output;
https://stackoverflow.com/questions/34680153
复制相似问题