2017-05-10 5 views
0

Ich habe eine API-Antwort wie folgt. Ich dies, um die Antwort zu deode. und bekam unter Antwort. $data = json_decode($response);extrahieren spezifische Daten aus Wikipedia api

{#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 ▶ 
      """ 
     } 
    } 
    } 
} 

Ich möchte title und content extrahieren. Wie kann ich das tun?

bearbeiten: Ich habe versucht, $data = json_decode($response, true); und unten Ergebnis erhalten, indem diese var_dump($data['query']['pages']) tun. Ergebnisse:

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 ▶ 
     """ 
    ] 
] 
+0

entfernen Bild und Einfügen von Code .... –

+0

https://stackoverflow.com/questions/7185288/how-to-get-wikipedia-content-using-wikipedias-api –

+0

Es antwortet nicht meine Frage @Ahmad. – vijayrana

Antwort

0

Verwenden Sie einfach Seiten als 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; 
} 
+0

hat es funktioniert. Vielen Dank – vijayrana

0

Verwenden formatversion=2 die Daten in ein leichter zu handhaben Format zu bekommen.

$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']);