php - 获取维基百科 API
问题描述
我正在尝试从我的网站的 wikipedia API 检索 json 数据
我们还必须附加一个pageid,而每个内容都有不同的pageid
维基百科上的 Whatsapp 有一个 pageid:686916
维基百科上的 Facebook 有一个 pageid:221230
和其他内容有不同的pageid
同时,要从 wikipedia api 中检索数据,必须包含一个 pageid
我该如何解决?
$ua = array();
$ua[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0';
$ua[] = 'content-type:application/json; charset=utf-8';
$data = json_decode(get("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=$query",$ua),true);
$getdata = $data["query"]["pages"]["pageidhere"]["extract"];
如果在 Python 中,使用以下逻辑查找 pageid
kya = request.args.get('q')
cih = f'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles={kya}'
heuh = get(cih).json()
heuh_ = heuh['query']['pages']
hueh = re.findall(r'(\d+)', str(heuh_))
result = heuh_[hueh[0]]['extract']:
查看以下行:
heuh_ = heuh['query']['pages']
hueh = re.findall(r'(\d+)', str(heuh_))
那么我该如何在 PHP 中实现呢?
解决方案
很抱歉打扰你,但你可以这样做
$ua = array();
$ua[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0';
$ua[] = 'content-type:application/json; charset=utf-8';
$data = json_decode(get("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=$query",$ua));
foreach ($data->query->pages as $pid) {
echo 'Your pageid = ' . $pid->pageid . PHP_EOL;
echo 'Title = ' . $pid->title . PHP_EOL;
echo 'extract = ' . $pid->extract . PHP_EOL;
}
结果
Your pageid = 7529378
Title = Facebook
extract = Facebook (stylized as facebook) is an American online social media and social networking service based in Menlo Park, California, and a flagship service of the namesake company Facebook, Inc. It was founded by Mark Zuckerberg, along with fellow Harvard College students and roommates Eduardo Saverin, Andrew McCollum, Dustin Moskovitz, and Chris Hughes.
The founders of Facebook initially limited membership to Harvard students. Membership was expanded to Columbia, Stanford, and Yale before being expanded to the rest of the Ivy League, MIT, and higher education institutions in the Boston area, then various other universities, and lastly high school students. Since 2006, anyone who claims to be at least 13 years old has been allowed to become a registered user of Facebook, though this may vary depending on local laws. The name comes from the face book directories often given to American university students.
Facebook can be accessed from devices with Internet connectivity, such as personal computers, tablets and smartphones. After registering, users can create a profile revealing information about themselves. They can post text, photos and multimedia which is shared with any other users that have agreed to be their "friend", or, with a different privacy setting, with any reader. Users can also use various embedded apps, join common-interest groups, buy and sell items or services on Marketplace, and receive notifications of their Facebook friends' activities and activities of Facebook pages they follow. Facebook claimed that it had 2.74 billion monthly active users as of September 2020, and it was the most downloaded mobile app of the 2010s globally.Facebook has been the subject of numerous controversies, often involving user privacy (as with the Cambridge Analytica data scandal), political manipulation (as with the 2016 U.S. elections), mass surveillance, psychological effects such as addiction and low self-esteem, and content such as fake news, conspiracy theories, copyright infringement, and hate speech. Commentators have accused Facebook of willingly facilitating the spread of such content and also exaggerating its number of users in order to appeal to advertisers. As of January 21, 2021, Alexa Internet ranks Facebook seventh in global internet usage.
注意:我删除了,true
所以 json 对象被转换为 PHP 对象
或者干脆
echo 'PageId = ' . array_keys((array)$data->query->pages)[0];
推荐阅读
- python-3.x - 使用 Python 提取以任何字符开头但不以 Rs 或 SBC 等字符/单词开头的行的正则表达式
- terraform - 在不同账户中担任角色时创建 EKS 失败
- java - 实现控制器通知全局异常处理程序,拦截运行时异常
- azure - Azure NSG 专用 IP 配置(白名单)Vnet 到 Vnet 通信
- javascript - 为什么 document.getElementById.value = "XYZ" 不写入输入文本字段?
- lua - 如何让roblox中的NPC正常四处走动,然后当它看到玩家追赶他们时
- embedded-linux - `APPEND` 变量不更新内核命令行
- spring - 我可以使用 YAML 文件为 spring.datasource.url 设置工作目录吗?
- c - 在 c 中获取用户输入时出现“访问被拒绝”错误
- reactjs - 为什么 usestate 以不同的顺序打印