How to cURL jSON data in PHP

This is something I always have to Google every time I need it so here it is.

If you are playing with API's it probably uses a GET method such as:

This kinds of API usually return jSON data, which is great. Here's how cURL this URL and parse the jSON data using PHP:

$url = "API-URL"; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); $result=curl_exec($ch); $json_results = json_decode($result, true); print_r($json_results);
Code language: PHP (php)

About Ricard Torres

Senior Front-end Software Engineer at Netcentric, from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> if you need to.