我正在尝试从维基百科 API 为我的网站检索 json 数据
我们还必须附加一个pageid,而每个内容都有一个不同的pageid
维基百科上的 Whatsapp 有一个 pageid:686916
维基百科上的 Facebook 有一个 pageid:221230
和其他内容有不同的pageid
同时,从维基百科 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中实现呢?
答案 0 :(得分:0)
很抱歉打扰您,但您可以这样做
$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];