Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cURL to get jSON data and decode the data?

Tags:

json

php

curl

api

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?

like image 672
Maaz Avatar asked May 22 '13 20:05

Maaz


People also ask

How do I decode a JSON file?

You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type. Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.

Can JSON parse Curl?

curl will not touch or parse the data that it sends, so you need to make sure it is valid JSON yourself. You can use multiple --json options on the same command line.

How can I get data from JSON?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.


1 Answers

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(); // 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 */]; 
like image 188
Wesley Schleumer de Góes Avatar answered Oct 01 '22 22:10

Wesley Schleumer de Góes