To extract data from a JSON response using cURL in PHP, follow these steps:
$ch = curl_init();
Set the CURLOPT_RETURNTRANSFER option to true to return the response instead of printing it. Specify the URL using CURLOPT_URL.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url);
Invoke curl_exec to execute the request and store the JSON response in the $result variable.
$result = curl_exec($ch);
Use json_decode to convert the JSON response into a PHP array.
$array = json_decode($result, true);
Access the data from the array using the appropriate keys. For example, to get the thread title with ID 13:
echo $array["threads"][13]["title"];
To get the message of the post with ID 23:
echo $array["threads"][13]["content"]["content"][23]["message"];
The above is the detailed content of How to Fetch and Decode JSON Data with cURL in PHP?. For more information, please follow other related articles on the PHP Chinese website!