projecte_ionic_entrega_02/backend/read_new.php

37 lines
1.2 KiB
PHP

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS');
header('Access-Control-Allow-Headers: token, Content-Type');
header('Access-Control-Max-Age: 1728000');
header('Content-Length:0');
header('Content-Type: text/plain');
$offset = $_GET["offset"];
$limit = $_GET["limit"];
$request_url = 'https://restcountries.com/v3.1/all';
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response);
$countries = array_slice($data, $offset, $limit);
$output = array();
foreach ($countries as $country) {
$obj = new stdClass();
$obj->flag = $country->flags->png;
$obj->name = $country->name->common;
$obj->capital = $country->capital[0];
$obj->continent = $country->continents[0];
$obj->area = $country->area;
$obj->population = $country->population;
$obj->drivingSide = $country->car->side;
array_push($output, $obj);
}
echo json_encode($output);
// echo json_encode($limit);
?>