How to call API from PHP to get jSON data and decode the data?

Mahabubur Rahman
0

 In PHP we can call API using cURL or file_get_content.

Using cURL:

//  Initiate curl

$ch = curl_init();

$url = "https://www.example.com";

// Set the url

curl_setopt($ch, CURLOPT_URL, $url);

// Will return the response, if false it print the response

curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

// Execute

$result = curl_exec($ch);

// Will dump a beauty json :3

$result_array = json_decode($result, true);

var_dump($result_array)

// Closing

curl_close($ch);

Using file_get_content:

$result = file_get_contents($url);

// Will dump a beauty json :3

var_dump(json_decode($result, true))


Post a Comment

0Comments
Post a Comment (0)