2017-05-10 1 views
0

다음과 같은 API 응답이 있습니다. 응답을 취소하려면 여기를 클릭하십시오. 응답을 얻지 못했습니다. $data = json_decode($response);위키 백과 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 ▶ 
      """ 
     } 
    } 
    } 
} 

나는 titlecontent을 추출 할. 어떻게해야합니까?

편집 : 나는 $data = json_decode($response, true);을 시도하고이 var_dump($data['query']['pages'])을 수행하여 그 결과 아래 얻을 수있다. 결과 :

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

제거 이미지 및 붙여 넣기 코드 .... –

+0

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

+0

그것은 응답하지 않습니다 내 질문 @Ahmad. – vijayrana

답변

0

그냥 배열로 페이지를 사용

$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

효과가 있습니다. 감사 – vijayrana

0

사용 formatversion=2가 쉽게 처리 할 수있는 형식으로 데이터를 얻을 수 있습니다.

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