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);Code language: PHP (php)

Leave a Reply

Your email address will not be published. Required fields are marked *