如何使用卷曲获取JSON数据和德code中的数据? [英] How to use cURL to get jSON data and decode the data?

查看:289
本文介绍了如何使用卷曲获取JSON数据和德code中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须返回一个JSON对象的链接,我需要它去codeD并投入PHP变量。

网址

<$p$p><$c$c>api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1

这是返回对象

  {
    伯爵:1,
    线程:{
        38752:{
            thread_id单:38752,
            NODE_ID:4,
            头衔:其ShadyCraft测试​​启动!
            REPLY_COUNT:45,
            VIEW_COUNT:946,
            USER_ID:2,
            用户名:黑幕
            POST_DATE:1366956695,
            粘性:0,
            discussion_state:可见,
            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:黑幕,
            prefix_id:19,
            tinhte_xentag_tags:一:4:{I:0; S:9:\\:秒; 2:4:I \\的Minecraft \\新闻\\; I:3; S:14:\\专用服务器\\我:1; S:10:\\shadycraft \\;}
            内容:{
                伯爵:1,
                内容:{
                    226167:{
                        POST_ID:226167,
                        thread_id单:38752,
                        USER_ID:2,
                        用户名:黑幕
                        POST_DATE:1366956695,
                        消息:[附件=全] 4143 [\\ / ATTACH] \\ n \\ nWe've完全重组ShadyCraft,今天将推出ShadyCraft公测\\ n \\ n目前的特点\\ n [LIST] \\ n [。 *]城镇\\ n [*]国际\\ n [*]所有出战争\\ n [*]一个实时更新的地图\\ n [*]没有白名单\\ n [*]赚大钱\\ n [*]恶意破坏允许的地方可能\\ n [\\ / LIST] \\ n这些都只是一些功能,其中有他们的背后有更多的东西。比如,有城镇和民族保养,税务,王国,市长和国王。\\ n \\ n我们很想有服务器自我管理,这就是为什么恶意破坏和PvP被允许在以往他们是可能的,所有的城镇和国家不能被其他成员griefed。您可以创建一个小镇,买地皮,并扩大了城镇如你所愿。\\ ñ\\ n [SIZE = 4]所有这一切都位于[URL =HTTP实时更新的地图所示:\\ / \\ / 50.7.6.116:8123']here[\\/URL].[\\/SIZE]\
\\ n [SIZE = 4] [SIZE = 6]加入测试版现在[\\ / SIZE] [\\ / SIZE] \\ n [SIZE = 6]服务器IP:50.7.6.116 [\\ / SIZE] \\ n \\ n \\ N [USER = 118053]法国式[\\ / USER]和[USER = 4863] Wolfbane [\\ / USER],
                        ip_id:747429,
                        message_state:可见,
                        attach_count:1,
                        位置:0,
                        喜欢: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:
                    }
                }
            }
        }
    }
}

我真的只在兴趣

 头衔:该ShadyCraft测试​​发射
REPLY_COUNT:45,
VIEW_COUNT:946,
USER_ID:2,
用户名:黑幕
POST_DATE:1366956695,
粘性:0,
discussion_state:可见,
discussion_open:1,

和最后的消息

  [附加=全] 4143 [/ ATTACH]我们已经完全重组ShadyCraft,今天将推出ShadyCraft公测。当前特点
[LIST]
[*]城镇
[*]国际
[*]所有出战争
[*]一个实时更新的地图
[*]没有白名单
[*]赚钱
[*]恶意破坏允许在可能的情况
[/ LIST]
这些都只是一些功能,其中有他们的背后有很多更多的事情。例如,有城镇和民族保养,税务,王国,市长,和国王。我们真的希望有服务器自我管理,这就是为什么恶意破坏和PvP被允许在以往他们是可能的。所有的城镇和国家不能被其他成员griefed。您可以创建一个小镇,买地皮,并扩大了城镇如你所愿。[SIZE = 4]这一切都显示在位于实时更新地图[URL =的http://50.7.6.116:8123']这里[/ URL] [/ SIZE][SIZE = 4] [SIZE = 6]加入测试版现在[/ SIZE] [/ SIZE]
[SIZE = 6]服务器IP:50.7.6.116 [/ SIZE]
[USER = 118053]法国式[/ USER]和[USER = 4863] Wolfbane [/ USER]

所以,我怎么可以提取JSON对象,并把它纠正PHP变量,那我以后可以使用?

变量,如: $用户名 $ USER_ID $消息 $标题 $ discussionState ,等等。

我只需要知道我怎么可以检索JSON对象,然后提取PHP中的数据转换成变量。

我现在能得到的PHP数组,但我有一些麻烦调用正确的价值观。这里是该阵列


  

阵列([统计] => 1 [主题] =>阵列([13] =>阵列([thread_id单]
  => 13 [NODE_ID] => 4 [标题] =>论坛整合接近完成! [REPLY_COUNT] => 0 VIEW_COUNT] => 0 [USER_ID] => 59 [用户名] =>
  Faeron [POST_DATE] => 1369257302 [置顶] => 0 discussion_state] =>
  可见[discussion_open] => 1 [discussion_type] => [first_post_id] =>
  23 first_post_likes] => 0 last_post_date] => 1369257302
  [last_post_id] => 23 last_post_user_id] => 59 last_post_username] =>
  Faeron [prefix_id] => 1 [内容] =>阵列([统计] => 1 [内容] =>
  阵列([23] =>阵列([POST_ID] => 23的thread_id] => 13 [USER_ID] =>
  59 [用户名] => Faeron [POST_DATE] => 1369257302 [消息] =>这是
  以来,我们开始整合phanime论坛有相当长一段时间
  主站点。现在我们已经完成了与phanime整合
  论坛和主站点。您将不再注意到,有两个
  平台上运行phanime,而是只有一个。我们的下一步是
  主题论坛,使它看起来像主网站! [ip_id] => 268
  [message_state] =>可见[attach_count] => 0 [位置] => 0 [喜欢]
  => 0 [like_users] =>一个:0:{} [warning_id] => 0 [WARNING_MESSAGE] =>))))))


现在让我们说这个数组被命名为 $阵列然后拿到的第一个元素的值[计数]我不能只说​​以下内容:打印$阵列[[计数]] &LT; - 这将返回一个错误。

怎么样,有一个值作为阵列本身,这是 [主题] 元素的元素。我如何获得,也许是 [thread_id单] 元素的值?


解决方案

我觉得这个人会回答你的问题:P

<$p$p><$c$c>$url=\"https://.../api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=‌​desc&limit=1&grab_content&content_limit=1\";

使用 卷曲

  //初始化卷曲
$ CH = curl_init();
//禁用SSL验证
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
//将返回应答,否则为false打印响应
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
//设置网址
curl_setopt($ CH,CURLOPT_URL,$网址);
// 执行
$结果= curl_exec($ CH);
//关闭
curl_close($ CH);//将转储美容JSON:3
后续代码var_dump(json_de code($结果,真实));

是<一个href=\"http://php.net/manual/pt_BR/function.file-get-contents.php\"><$c$c>file_get_contents

  $结果=的file_get_contents($网址);
//将转储美容JSON:3
后续代码var_dump(json_de code($结果,真实));

访问

  $阵列[线程] [13 / *线程ID * /] [称号/ *线程键* /]

  $阵列[线程] [13 / *线程ID * /] [内容/ *线程键* /] [内容] [23 / *帖子ID * /] [消息/ *内容密钥* /]

So I have a link that returns a jSON object, and I need to have it decoded and put into variables in PHP.

URL

api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1

This is the object that it returns

{
    "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": ""
                    }
                }
            }
        }
    }
}

I am really only interested in the

"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,

and finally the message

[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]

So how can I extract the jSON object and put it in to correct variables in PHP, that I can later use?

Variables like: $username, $user_id, $message, $title, $discussionState, and so on.

I just need to know how I can retrieve the jSON object, then extract the data into variables in PHP.

I am now able to get the PHP array, but I am having some troubles calling the correct values. Here is the array.

Array ( [count] => 1 [threads] => Array ( [13] => Array ( [thread_id] => 13 [node_id] => 4 [title] => Forum Integration nearly complete! [reply_count] => 0 [view_count] => 0 [user_id] => 59 [username] => Faeron [post_date] => 1369257302 [sticky] => 0 [discussion_state] => visible [discussion_open] => 1 [discussion_type] => [first_post_id] => 23 [first_post_likes] => 0 [last_post_date] => 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] => It's been quite a while since we began to integrate the phanime Forums with the main site. We have now finished the integration with the phanime Forums and the main site. You will no longer notice that there are two platforms running phanime, but instead only one. Our next step is to theme the forums to make it look like the main site! [ip_id] => 268 [message_state] => visible [attach_count] => 0 [position] => 0 [likes] => 0 [like_users] => a:0:{} [warning_id] => 0 [warning_message] => ) ) ) ) ) )

Now lets say this array was named $array then to get the first element's value "[count]" can't I just say the following: print $array["[count]"] <-- this returns an error.

What about the element that has a value as an array itself, which is the [threads] element. How do I get, perhaps the [thread_id] element's value?

解决方案

I think this one will answer your question :P

$url="https://.../api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=‌​desc&limit=1&grab_content&content_limit=1";

Using cURL

//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));

Using file_get_contents

$result = file_get_contents($url);
// Will dump a beauty json :3
var_dump(json_decode($result, true));

Accessing

$array["threads"][13/* thread id */]["title"/* thread key */]

And

$array["threads"][13/* thread id */]["content"/* thread key */]["content"][23/* post id */]["message" /* content key */];

这篇关于如何使用卷曲获取JSON数据和德code中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆