所以我有一个返回JSON对象的链接,我需要将其解码并放入PHP中的变量中。
网址
api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1
这是它返回的对象
{
"count": 1,
"threads": {
"38752": {
"thread_id": 38752,
"node_id": 4,
"title": "The ShadyCraft Beta Launch!",
"reply_count": 45,
"view_count": 946,
"user_id": 2,
"username": "Shady",
"post_date": 1366956695,
"sticky": 0,
"discussion_state": "visible",
"discussion_open": 1,
"discussion_type": "",
"first_post_id": 226167,
"first_post_likes": 7,
"last_post_date": 1369094302,
"last_post_id": 228226,
"last_post_user_id": 2,
"last_post_username": "Shady",
"prefix_id": 19,
"tinhte_xentag_tags": "a:4:{i:0;s:9:\"minecraft\";i:2;s:4:\"news\";i:3;s:14:\"private server\";i:1;s:10:\"shadycraft\";}",
"content": {
"count": 1,
"content": {
"226167": {
"post_id": 226167,
"thread_id": 38752,
"user_id": 2,
"username": "Shady",
"post_date": 1366956695,
"message": "[ATTACH=full]4143[\/ATTACH]\n \nWe've completely restructured ShadyCraft, and today will be the launch of the ShadyCraft beta.\n \nCurrent Features\n[LIST]\n[*]Towns\n[*]Nations\n[*]All out wars\n[*]A live update map\n[*]No White-list\n[*]Earn Money\n[*]Griefing allowed where possible\n[\/LIST]\nThese are just some features which have a lot more things behind them. For instance, there is town and nation upkeep, tax, kingdoms, mayors, and kings.\n \nWe really wanted to have the server self-governed and this is why griefing and PvP are allowed where ever they are possible. All towns and nations cannot be griefed by other members. You can create a town and buy plots for it and expand the town as you wish.\n \n[SIZE=4]All of this is shown in the live updating map located [URL='http:\/\/50.7.6.116:8123']here[\/URL].[\/SIZE]\n \n[SIZE=4][SIZE=6]Join the Beta Now[\/SIZE][\/SIZE]\n[SIZE=6]Server-IP: 50.7.6.116[\/SIZE]\n \n \n[USER=118053]Frenchy[\/USER] and [USER=4863]Wolfbane[\/USER]",
"ip_id": 747429,
"message_state": "visible",
"attach_count": 1,
"position": 0,
"likes": 7,
"like_users": "a:5:{i:0;a:2:{s:7:\"user_id\";i:105699;s:8:\"username\";s:6:\"kvothe\";}i:1;a:2:{s:7:\"user_id\";i:146724;s:8:\"username\";s:12:\"Graveyard219\";}i:2;a:2:{s:7:\"user_id\";i:70182;s:8:\"username\";s:9:\"wmbrown18\";}i:3;a:2:{s:7:\"user_id\";i:5473;s:8:\"username\";s:9:\"Oblivious\";}i:4;a:2:{s:7:\"user_id\";i:118053;s:8:\"username\";s:7:\"Frenchy\";}}",
"warning_id": 0,
"warning_message": "",
"anonymous_posting_real_user_id": 0,
"anonymous_posting_real_username": ""
}
}
}
}
}
}
我真的只对
"title":"The ShadyCraft Beta Launch!",
"reply_count:"45,
"view_count": 946,
"user_id":2,
"username":"Shady",
"post_date":1366956695,
"sticky":0,
"discussion_state":"visible",
"discussion_open":1,
最后,
[ATTACH=full]4143[/ATTACH]
We've completely restructured ShadyCraft, and today will be the launch of the ShadyCraft beta.
Current Features
[LIST]
[*]Towns
[*]Nations
[*]All out wars
[*]A live update map
[*]No White-list
[*]Earn Money
[*]Griefing allowed where possible
[/LIST]
These are just some features which have a lot more things behind them. For instance, there is town and nation upkeep, tax, kingdoms, mayors, and kings.
We really wanted to have the server self-governed and this is why griefing and PvP are allowed where ever they are possible. All towns and nations cannot be griefed by other members. You can create a town and buy plots for it and expand the town as you wish.
[SIZE=4]All of this is shown in the live updating map located [URL='http://50.7.6.116:8123']here[/URL].[/SIZE]
[SIZE=4][SIZE=6]Join the Beta Now[/SIZE][/SIZE]
[SIZE=6]Server-IP: 50.7.6.116[/SIZE]
[USER=118053]Frenchy[/USER] and [USER=4863]Wolfbane[/USER]
那么,我如何提取JSON对象并将其放入PHP中以纠正变量,以便以后使用?
变量如:$username
、$user_id
、$message
、$title
、$discussionState
等等。
我只需要知道如何检索JSON对象,然后将数据提取到PHP变量中。
我现在可以得到PHP数组了,但是在调用正确的值时遇到了一些问题。这里是数组。
Array([count] => 1 [threads] => Array([13] => Array([thread_id] => 13 [node_id] => 4 [title] =>论坛集成即将完成![回复数量] => 0 [查看数量] => 0 [用户ID] => 59 [用户名] => Faeron [发布日期] => 1369257302 [粘性] => 0 [讨论状态] =>可见[讨论打开] => 1 [讨论类型] => [第一个帖子ID] => 23 [第一个帖子喜欢] => 0 [最后一个帖子日期] => 1369257302 [last_post_id] => 23 [last_post_user_id] => 59 [last_post_username] => Faeron [prefix_id] => 1 [content] => Array([count] => 1 [content]=> Array([23] => Array([post_id] => 23 [thread_id] => 13 [user_id] => 59 [username] => Faeron [post_date] => 1369257302 [message] =>自从我们开始将phanime论坛与主站点整合以来,已经有相当长的一段时间了。我们现在已经完成了与phanime论坛和主网站的整合。您将不再注意到有两个平台运行phanime,而是只有一个。我们的下一步是主题的论坛,使它看起来像主网站![IP_ID] => 268 [消息状态] =>可见[attach_count] => 0 [位置] => 0 [喜欢] => 0 [喜欢_用户] => a:0:{} [警告_ID] => 0 [警告_消息] =>)
现在让我们假设这个数组名为$array
,那么要获取第一个元素的值“[count]”,我不能只说下面的话:print $array["[count]"]
<--返回错误。
那么本身具有数组值的元素,也就是[threads]
元素呢?如何获取[thread_id]
元素的值?
6条答案
按热度按时间8wtpewkr1#
我想这一个会回答你的问题:P
使用
cURL
使用
file_get_contents
我知道了
然后
kg7wmglp2#
你可以用这个来做Curl:
阅读JSON:
然后又道:
你也可以使用这个:
希望这对你有帮助。
kiz8lqtg3#
要获取对象,你不需要使用cURL(你正在加载另一个dll到内存中,并且有另一个依赖项,除非你真的需要curl,我会坚持使用内置的php函数),你可以使用一个简单的php file_get_contents(url)函数:http://il1.php.net/manual/en/function.file-get-contents.php
然后json_decode()将JSON解析为PHP对象,如果将
true
传递给第二个参数,则解析为数组。http://php.net/manual/en/function.json-decode.php举例来说:
xpszyzbs4#
您还可以使用
roqulrg35#
使用此功能:http://br.php.net/json_decode这将自动创建PHP数组。
piah890a6#
你可以使用这个: