php


extract specific data from wikipedia api


I have got a API response as below.
I this this to deode the response. and got below response.
$data = json_decode($response);
{#240 ▼
+"batchcomplete": ""
+"query": {#243 ▼
+"pages": {#234 ▼
+"171166": {#245 ▼
+"pageid": 171166
+"ns": 0
+"title": "Nepal"
+"extract": """
Nepal (/nəˈpɔːl/; Nepali: नेपाल Nepāl [neˈpal]), officially the Federal Democratic Republic of Nepal (Nepali: सङ्घीय लोकतान्त्रिक गणतन्त्र नेपाल Sanghiya Loktā ▶
The territory of Nepal has a recorded history since the Neolithic age. The name "Nepal" is first recorded in texts from the Vedic Age, the era which founded Hin ▶
Modern Nepal is a federal secular parliamentary republic. It has seven states. Nepal is a developing nation, ranking 144th on the Human Development Index (HDI) ▶
Nepal's foreign relations expanded after the Anglo-Nepal Treaty of 1923, which was recognized by the League of Nations. After a Soviet veto in 1949, Nepal was a ▶
"""
}
}
}
}
I want to extract title and content. How may I do that?
Edit: I have tried $data = json_decode($response, true); and get below result by doing this var_dump($data['query']['pages']). Results:
array:1 [▼
171166 => array:4 [▼
"pageid" => 171166
"ns" => 0
"title" => "Nepal"
"extract" => """
Nepal (/nəˈpɔːl/; Nepali: नेपाल Nepāl [neˈpal]), officially the Federal Democratic Republic of Nepal (Nepali: सङ्घीय लोकतान्त्रिक गणतन्त्र नेपाल Sanghiya Loktā ▶
The territory of Nepal has a recorded history since the Neolithic age. The name "Nepal" is first recorded in texts from the Vedic Age, the era which founded Hin ▶
Modern Nepal is a federal secular parliamentary republic. It has seven states. Nepal is a developing nation, ranking 144th on the Human Development Index (HDI) ▶
Nepal's foreign relations expanded after the Anglo-Nepal Treaty of 1923, which was recognized by the League of Nations. After a Soviet veto in 1949, Nepal was a ▶
"""
]
]
Just use pages as array:
$response = json_decode(file_get_contents('https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=Stack%20Overflow'));
$pages = (array) $response ->query->pages;
foreach ($pages as $id => $page) {
echo $page->title;
}
Use formatversion=2 to get the data in an easier to handle format.
$response = json_decode(file_get_contents('https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&formatversion=2&exintro=&titles=Stack%20Overflow'), true);
echo($response['query']['pages'][0]['title']);

Related Links

Programatically Export Excel file from an MS Access table Using PHP
How to send Cookie using PHP Goutte
How can I hide affiliate URL before redirecting to actual store URL using PHP?
Crack Own Password Database Feasibility
require won't load variables from current file
How to wrap Wordpress wpautop in a container with a class for CSS manipulation
handle Request cannot be performed
How to acccess to another view in CodeIgniter?
Getting posts related to a keyword
Can we give bindvariables in php-oracle direclty in the Query?
Search + Sort in related model in GridView (yii2)
Multi step form - ajax response on the following step
Why Php Media Gallery not working?
Create html table with arrays and json data
Failed to move uploaded file to online server in php
Connecting to MSSQL from Doctrine/Symfony2

Categories

HOME
focus
long-integer
liferay-7
capistrano
elastic-load-balancer
driver
solution
heuristics
locationmanager
pushdown-automaton
jodatime
dhcp
trace32
jest
data-type-conversion
altium-designer
connection-refused
plyr
code-climate
substance
source-insight
location-services
riotjs
john-the-ripper
rspec-rails
java-stream
love2d
reset
ejml
expandablelistview
sendinput
destroy
rm
winston
embedded-v8
ptvs
glimpse
network-protocols
ltrace
rustdoc
prefetch
scheduledexecutorservice
multi-touch
zynq
bbc-micro
noclassdeffounderror
android-async-http
psd
ioexception
deeplink
lowercase
android-studio-import
android-handler
kefir.js
clarion
console.log
nssortdescriptor
mod-auth-openidc
symja
configurable-product
window-resize
visual-c++-2012
otl
assetic
armv6
oscilloscope
ui-design
nsmutablestring
workflow-manager-1.x
image-formats
coordinate
kaazing
n-queens
yuidoc
django-apps
drawimage
groovy-console
representation
css-friendly
mongrel2
ios-4.2
resharper-5.1
dm
tacit-programming
stretchblt
service-factory

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App