当前位置: 首页 > 面试题库 >

如何使用cURL获取jSON数据并解码数据?

阙俊友
2023-03-14
问题内容

因此,我有一个返回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数组,但是在调用正确的值时遇到了一些麻烦。这是数组。

自从我们开始将幻象论坛与主要站点集成以来,已经有一段时间了。现在,我们已经完成了与幻象论坛和主站点的集成。您将不再注意到有两个运行幻影的平台,而只有一个。我们的下一步是主题论坛,使其看起来像主站点![ip_id]
=> 268 [message_state] =>可见[attach_count] => 0 [位置] => 0 [likes] => 0
[like_users] => a:0:{} [warning_id] => 0 [warning_message] =>))))))

现在让我们说这个数组被命名$array为获取第一个元素的值“ [count]”,我不能只说以下内容:print $array["[count]"]
<-这将返回错误。

那么具有本身作为数组值的[threads]元素呢?我如何获得[thread_id]元素的价值?


问题答案:

我认为这将回答您的问题: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";

使用cURL

//  Initiate curl
$ch = curl_init();
// 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));

使用file_get_contents

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

存取中

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

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


 类似资料:
  • 所以我有一个返回jSON对象的链接,我需要对它进行解码并将其放入PHP中的变量中。 URL 这是它返回的对象 我真的只对 最后是信息 那么,我如何提取jSON对象并将其放入PHP中以更正我以后可以使用的变量呢? 像这样的变量:,,,,,等等。 我只需要知道如何检索jSON对象,然后将数据提取到PHP中的变量中。 我现在能够获得PHP数组,但是调用正确的值时遇到了一些问题。这是数组。 数组([计数]

  • 问题内容: 我使用Ubuntu并在其上安装了cURL。我想用cURL测试我的Spring REST应用程序。我在Java端编写了POST代码。但是,我想用cURL对其进行测试。我正在尝试发布JSON数据。示例数据如下: 我使用以下命令: 它返回此错误: 错误描述是这样的: 服务器拒绝了此请求,因为请求实体的格式不受请求方法()的请求资源支持。 Tomcat日志:“ POST / ui / weba

  • 之后,服务器将通过URL地址给出响应作为授权代码(我已经完成了这一步,并得到了代码)。 正如docs解释的那样,代码应该使用/oauth2/access_token交换访问令牌,只要有客户机ID和客户机Secret(get access_token) 但是我不知道如何通过cURL将数据发送到服务器,我尝试了很多curl_setopt()选项和组合,但仍然一无所获。

  • 输出为 所以,即使在我自己的服务器上,我也没有得到正确的数据,它是空数组。我想通过http://docs.shopify.com/api/customer#create使用json实现REST

  • 我有以下代码: 它返回json结果后的示例,但不返回访问令牌(尽管json_解码)。curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);不返回下面的令牌。谢谢你帮助我。

  • 本文向大家介绍PHP中使用CURL模拟登录并获取数据实例,包括了PHP中使用CURL模拟登录并获取数据实例的使用技巧和注意事项,需要的朋友参考一下 cURL 是一个功能强大的PHP库,使用PHP的cURL库可以简单和有效地抓取网页并采集内容,设置cookie完成模拟登录网页,curl提供了丰富的函数,开发者可以从PHP手册中获取更多关于cURL信息。本文以模拟登录开源中国(oschina)为例,和