所以我有一个返回jSON对象的链接,我需要将它解码并放入PHP中的变量中。
URL
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数组,但在调用正确的值时遇到了一些问题。这是数组。
阵列(计数=> 1线程=>阵列( 13 =>阵列( thread_id => 13 node_id => 4 title =>论坛集成即将完成!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 1369257302 23 # 59 # Faeron 1 content数组(计数1内容数组( 23数组( 23 13user_id => 59 username => Faeron post_date => 1369257302 message =>自从我们开始将phanime论坛与主站点整合以来,已经有一段时间了。我们现在已经完成了与phanime论坛和主站点的集成。你不会再注意到有两个平台在运行phanime,而是只有一个。我们的下一步是主题论坛,使其看起来像主网站!ip_id => 268 message_state => visible attach_count => 0 position => 0 likes => 0 like_users => a:0:{} warning_id => 0 warning_message => )))
现在假设这个数组被命名为$array
,然后为了获得第一个元素的值"count“,我不能只说:print $array["[count]"]
<--这会返回一个错误。
对于值为数组本身的元素,它是[threads]
元素。如何获取[thread_id]
元素的值?
发布于 2013-05-23 04:47:32
我想这将会回答你的问题:
$url="https://.../api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1";
使用 的
// 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));
使用 的
$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 */];
发布于 2013-05-23 04:26:13
要获得不需要使用cURL的对象(您正在将另一个dll加载到内存中并具有另一个依赖项,除非您确实需要curl,否则我将坚持使用内置的php函数),您可以使用一个简单的php (File_get_contents)函数:http://il1.php.net/manual/en/function.file-get-contents.php
$unparsed_json = file_get_contents("api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1");
$json_object = json_decode($unparsed_json);
然后json_decode()将JSON解析为一个PHP对象,如果将true
传递给第二个参数,则解析为一个数组。http://php.net/manual/en/function.json-decode.php
例如:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json)); // Object
var_dump(json_decode($json, true)); // Associative array
发布于 2014-08-10 17:59:17
您可以对Curl使用此命令:
function fakeip()
{
return long2ip( mt_rand(0, 65537) * mt_rand(0, 65535) );
}
function getdata($url,$args=false)
{
global $session;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: ".fakeip(),"X-Client-IP: ".fakeip(),"Client-IP: ".fakeip(),"HTTP_X_FORWARDED_FOR: ".fakeip(),"X-Forwarded-For: ".fakeip()));
if($args)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$args);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8888");
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
然后阅读Json:
$result=getdata("https://example.com");
然后:
///Deocde Json
$data = json_decode($result,true);
///Count
$total=count($data);
$Str='<h1>Total : '.$total.'';
echo $Str;
//You Can Also Make In Table:
foreach ($data as $key => $value)
{
echo ' <td><font face="calibri"color="red">'.$value[type].' </font></td><td><font face="calibri"color="blue">'.$value[category].' </font></td><td><font face="calibri"color="green">'.$value[amount].' </font></tr><tr>';
}
echo "</tr></table>";
}
您还可以使用以下命令:
echo '<p>Name : '.$data['result']['name'].'</p>
<img src="'.$data['result']['pic'].'"><br>';
希望这能帮上忙。
https://stackoverflow.com/questions/16700960
复制相似问题