Fetching API with curl PHP

Viewed 107

I want to fetch API from https://www.travel-advisory.info/api then I already write my code

  $curl = new CurlService();
  $response = $curl->to('https://www.travel-advisory.info/api')->get();
  throw_if(!$response, Exception::class, 'Terjadi kesalahan: Data tidak dapat diperoleh');      
  $data = json_decode($response);
  echo $data->data;

so this is example response from API

  "data": {
    "AD": {
      "iso_alpha2": "AD",
      "name": "Andorra",
      "continent": "EU",
      "advisory": {
        "score": 2.79999999999999982236431605997495353221893310546875,
        "sources_active": 4,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/andorra"
      }
    },
    "AE": {
      "iso_alpha2": "AE",
      "name": "United Arab Emirates",
      "continent": "AS",
      "advisory": {
        "score": 3.100000000000000088817841970012523233890533447265625,
        "sources_active": 7,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/united-arab-emirates"
      }
    },
    "AF": {
      "iso_alpha2": "AF",
      "name": "Afghanistan",
      "continent": "AS",
      "advisory": {
        "score": 5,
        "sources_active": 10,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/afghanistan"
      }
    },
    "AG": {
      "iso_alpha2": "AG",
      "name": "Antigua and Barbuda",
      "continent": "NA",
      "advisory": {
        "score": 3,
        "sources_active": 3,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/antigua-and-barbuda"
      }
    },
    "AI": {
      "iso_alpha2": "AI",
      "name": "Anguilla",
      "continent": "NA",
      "advisory": {
        "score": 3,
        "sources_active": 3,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/anguilla"
      }
    }
  }

my question is how to filter continent : "AS" ? p

3 Answers
$data = json_decode($response, true)['data'];
print_r($this->continentFilter($data, 'AS'));

The function will be like:

private function continentFilter(array $data, string $continent): array
{
    $filteredArray = [];
    foreach ($data as $item) {
        if ($item['continent'] === $continent) {
            $filteredArray = $item;

            break;
        }
    }

    return $filteredArray;
}

You can loop through each object and push to a new array, if the continent value matches "AS".

Using $key => $value allows you to retain the indexes.

Example:

$filtered = [];
foreach ( $data->data as $key => $value )
{
    if ( $value->continent === "AS" )
    {
        $filtered[$key] = $value;
    }
}

print_r($filtered);

Will give you:

[AE] => stdClass Object
    (
        [iso_alpha2] => AE
        [name] => United Arab Emirates
        [continent] => AS
        [advisory] => stdClass Object
            (
                [score] => 3.1
                [sources_active] => 7
                [message] => 
                [updated] => 2020-08-04 07:21:19
                [source] => https://www.travel-advisory.info/united-arab-emirates
            )

    )

[AF] => stdClass Object
    (
        [iso_alpha2] => AF
        [name] => Afghanistan
        [continent] => AS
        [advisory] => stdClass Object
            (
                [score] => 5
                [sources_active] => 10
                [message] => 
                [updated] => 2020-08-04 07:21:19
                [source] => https://www.travel-advisory.info/afghanistan
            )

    )
    ...

you can use json_decode with array flag and array_filter method like this:

$data = (json_decode($response, true))['data'];

$as = array_filter($data, function($item) {
    return $item['continent'] === 'AS';
});

Also please note, that the json you give as example above needs to wrapped in curly braces all together, like this:

{
  "data": {
  ...
}
Related