Php 使用id标识符显示JSON的项
我有一个JSON格式的博客API,我正试图与PHP一起使用。我有一个名为“index.php”的页面,其中列出了从JSON文件中提取的博客中的所有帖子,并将id从指向该页面的href链接传递到另一个页面“post.php” 我需要“post.php”页面来保存一篇文章,当用户单击所选文章时,由“index.php”发送的id标识 我正在显示标题和其他属性,如下所示:Php 使用id标识符显示JSON的项,php,json,Php,Json,我有一个JSON格式的博客API,我正试图与PHP一起使用。我有一个名为“index.php”的页面,其中列出了从JSON文件中提取的博客中的所有帖子,并将id从指向该页面的href链接传递到另一个页面“post.php” 我需要“post.php”页面来保存一篇文章,当用户单击所选文章时,由“index.php”发送的id标识 我正在显示标题和其他属性,如下所示: <h2><?php echo $post->title ?></h2> 理想情况下,您
<h2><?php echo $post->title ?></h2>
理想情况下,您使用的博客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数组更改为JavaScript对象。使用此功能,您现在可以通过$post->id->title
编辑 根据您的评论,您可以按如下方式使用api: $data=json_解码(文件获取内容(“”));
echo$data->title 理想情况下,您使用的博客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数组更改为JavaScript对象。使用此功能,您现在可以通过$post->id->title
编辑 根据您的评论,您可以按如下方式使用api: $data=json_解码(文件获取内容(“”));
echo$data->title 你想从这个json中提取什么?嗨,抱歉,不确定你的意思我需要标题、日期和内容更新我的帖子,你可以检查。你想从这个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
}