且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-21 20:53:29

我想这个会回答你的问题 :P

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

使用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 */];