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:

http://the-api.com/method/ID/get-results

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

About Rick

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

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*