2017-04-15 5 views
1

JSON에서 PHP와 함께 사용하려고하는 블로그 API가 있습니다. JSON 파일에서 가져온 블로그의 모든 게시물을 나열하고 해당 페이지의 href 링크에서 다른 페이지 'post.php'에 ID를 전달하는 'index.php'라는 페이지가 있습니다.id 식별자를 사용하는 JSON 항목 표시

나는 사용자가 I는 다음과 같이 제목과 다른 속성을 표시하고 선택한 후

를 클릭 할 때 '의 index.php'에서 전송 된 ID로 식별 한 게시물을 유지하기 위해 'post.php'페이지가 필요합니다 :

<h2><?php echo $post->title ?></h2> 

는하지만 아래는이며 지정된 ID 만 게시물이 표시되도록

<h2><?php echo $post->id->$post->title ?></h2> 

같은 것을 할 필요가

{ 


"posts": [ 
    { 
     "type": "post", 
     "date": "2017-04-11T13:36:46+00:00", 
     "title": "Title here", 

     "content": "my content"  
     "author": { 
     "id": 878, 
     "nicename": "tretr", 
     "display_name": "name here", 
     "user_url": "", 
     "posts_url": "https:\\/\\/blogs.kent.ac.uk\\/kbs-news-events\\/author\\/cmb58\\/", 
     "meta": { 
      "description": "", 
      "first_name": "first name", 
      "last_name": "last name", 

     } 
     }, 

     "id": 1234, 
     "permalink": "https:\/\/ link.....", 
     "modified": "2017-04-11T13:39:36+00:00", 
     "excerpt": "more here", 
     "meta": [], 

     etc....... 
    } 
    ], 
    "http_status": 200 
} 
+0

이 json에서 추출 할 항목을 선택하십시오. –

+0

안녕하세요 죄송합니다 무슨 뜻인지 모르겠다 – wilky

+0

제목, 날짜 및 내용이 필요합니다 – wilky

답변

0

이상한 블로그 API는 ID 기반 레코드를 반환해야하므로 하나의 게시물을 반환하는보기 API와보기 API가 있어야합니다. 하지 않으면, 나는 통해 루프 조언이 같은 것을 현재 JSON 구조의 형식을 변경할 수 있습니다 :

{ 
    "posts": { 
     "1234": { 
      "type": "post", 
      "date": "2017-04-11T13:36:46+00:00", 
      "title": "Title here", 
      "content": "my content", 
      "author": { 
       "id": 878, 
       "nicename": "tretr", 
       "display_name": "name here", 
       "user_url": "", 
       "posts_url": "https:\\/\\/blogs.kent.ac.uk\\/kbs-news-events\\/author\\/cmb58\\/", 
       "meta": { 
        "description": "", 
        "first_name": "first name", 
        "last_name": "last name" 
       } 
      }, 
      "id": 1234, 
      "permalink": "https:// link.....", 
      "modified": "2017-04-11T13:39:36+00:00", 
      "excerpt": "more here", 
      "meta": [] 
     } 
    }, 
    "http_status": 200 
} 

을 주목 "1234" : {} 키가 자바 스크립트 객체로 각 게시물 배열을 변경했다. 다음과 같이 사용하여, 당신은 지금 당신의 의견에 따라 경찰 $post->id->title


편집 에 의해 각 게시물에 액세스 할 수 있습니다, 당신은 API를 사용할 수 있습니다 $ 데이터 = json_decode (file_get_contents ('http://api.kent.ac.uk/api/v1/blogs/music-matters/8192')); echo $ data-> title;

+0

안녕하세요. https://api.kent.ac.uk/api/v1/blogs/music-matters/8192로 개별 게시물을 반환 할 수 있습니까? – wilky

+0

내 대답을 업데이트하여 사용법을 보여줍니다. – gaganshera