I'm trying to integrate with the REST Countries V2 API to pull country data and display it on a webpage I'm making. I was pointed in the direction of curl so I have updated the question to what I am trying.
So far I have this code:
<?
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
$options = array(CURLOPT_URL => 'https://restcountries.com/v3.1/all',
CURLOPT_HEADER => false
);
curl_setopt_array($ch, $options);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Its pulling an enormous JSON list onto my page. I'm not super familiar with JSON, how can I target the specific pieces of data such as the country name. Is it possible to do with with php as I'm more familiar with php than javascript and I would like to make a nice grid layout using a php foreach loop with each country having its own card.
CodePudding user response:
try curl
$ch = curl_init("https://restcountries.com/v2/all");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response=json_decode($response);
foreach ($response as $country){
echo $country->name;
echo $country->flag;
}