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

POST a SESSION variable on another page
Error in Gii Code Generator Page
Can I display all the cookies I set in PHP?
Curl, NuSoap, PHP talking to Exchange server authentication error
Issue with mysql_real_escape_string
jquery datepicker manipulation
Reading and printing user inputs in HTML in PHP
Using cURL to login into third party sites - Where to keep the cookie
PHP ::: Speed Test ::: $_SESSION vs. $variable
Global Temporary Table Creation Failed
Add some logo to all images in folder
How to call a php function? [closed]
What does double INF mean in PHP warning?
Can't search for key into associative array
Javascript generate select list value not available in the next page
Too many query to database in Symfony and Doctrine

Categories

HOME
xpath
knockout.js
opencart
facebook-messenger
jms
d3.js
compilation
dafny
ios-simulator
histogram
web2py
liferay-7
iptables
navigation
supercollider
hspi
sap-fiori
game-maker
mpu6050
outlook-restapi
stimulsoft
synthesis
pygobject
c#-7.0
scala-ide
codefluent
wsf
bluemix-mobile-services
tableview
fluent-migrator
quartz.net
xdebug
adapter
javax.persistence
materialize
fusion
google-play-developer-api
windows2012
memory-fences
serversocket
landscape
cordys-opentext
nanogallery
twilio-php
identify
cloudera-quickstart-vm
praat
game-center
opal-framework
oracle-bmcs
reverse-dns
audio-converter
music21
supertest
nonlinear-functions
password-hash
renderman
solr-query-syntax
webvtt
mechanicalturk
haraka
thread-exceptions
clp
dmarc
ftp-client
nsdata
android-sdcard
right-to-left
noclassdeffounderror
psd
openmrs
flush
ioctl
gdl
phpquery
in-app-billing
nastran
libz
sablecc
python-c-extension
kognitio-wx2
adwords-apiv201402
accessory
wso2bam
apache-shindig
notifyjs
coalesce
jacob
arel
jbox2d
ntlmv2
significant-digits
xcode4.5
live-wallpaper
exponent
zend-server-ce
servercontrol
will-paginate
kohana-auth
digiflow
bll
eaccelerator
mongrel
aggregator
ppc
external-sorting
efs
objectinstantiation
acceleration
levels
parentid

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