使用curl PHP 获取API

我想从那时起获取 APIhttps://www.travel-advisory.info/api我已经编写了我的代码


  $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;

这是 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"

      }

    }

  }

我的问题是如何过滤continent : "AS"?p


吃鸡游戏
浏览 90回答 3
3回答

慕雪6442864

$data = json_decode($response, true)['data'];print_r($this->continentFilter($data, 'AS'));该函数将类似于:private function continentFilter(array $data, string $continent): array{    $filteredArray = [];    foreach ($data as $item) {        if ($item['continent'] === $continent) {            $filteredArray = $item;            break;        }    }    return $filteredArray;}

杨魅力

您可以使用json_decode数组标志和array_filter方法,如下所示:$data = (json_decode($response, true))['data'];$as = array_filter($data, function($item) {    return $item['continent'] === 'AS';});另请注意,上面示例中给出的 json 需要全部用大括号括起来,如下所示:{  "data": {  ...}

12345678_0001

如果大陆值与“AS”匹配,您可以循环遍历每个对象并推送到新数组。使用$key => $value允许您保留索引。例子:$filtered = [];foreach ( $data->data as $key => $value ){    if ( $value->continent === "AS" )    {        $filtered[$key] = $value;    }}print_r($filtered);会给你:[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            )    )    ...
打开App,查看更多内容
随时随地看视频慕课网APP