首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Echo JSON PHP数组WIKI API

Echo JSON PHP数组WIKI API
EN

Stack Overflow用户
提问于 2016-01-08 23:09:54
回答 2查看 51关注 0票数 0

我不知道导出API的对象是谁

因此,URL是:

https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=200&titles=Jean-Claude%20Van%20Damme

我得到了

代码语言:javascript
运行
复制
 {"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"}}}}

谁只能导出源码?

现在我有了下面的代码:

代码语言:javascript
运行
复制
$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'];

那么我是做谁的呢?

EN

回答 2

Stack Overflow用户

发布于 2016-01-08 23:30:56

您可以使用json_decode并将true作为第二个参数传递。

从文档中:

如果为TRUE,则返回的对象将转换为关联数组。

那么我想你要找的是:

代码语言:javascript
运行
复制
$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;

将导致:

https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Jean-Claude_Van_Damme_2012.jpg/141px-Jean-Claude_Van_Damme_2012.jpg

票数 0
EN

Stack Overflow用户

发布于 2016-01-08 23:46:51

尝尝这个。

代码语言:javascript
运行
复制
$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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34680153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档