为了账号安全,请及时绑定邮箱和手机立即绑定

使用curl PHP 获取API

使用curl PHP 获取API

PHP
吃鸡游戏 2023-09-15 10:16:56
我想从那时起获取 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
查看完整描述

3 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

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

}



查看完整回答
反对 回复 2023-09-15
?
杨魅力

TA贡献1811条经验 获得超6个赞

您可以使用json_decode数组标志和array_filter方法,如下所示:


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


$as = array_filter($data, function($item) {

    return $item['continent'] === 'AS';

});

另请注意,上面示例中给出的 json 需要全部用大括号括起来,如下所示:


{

  "data": {

  ...

}


查看完整回答
反对 回复 2023-09-15
?
12345678_0001

TA贡献1802条经验 获得超5个赞

如果大陆值与“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

            )


    )

    ...


查看完整回答
反对 回复 2023-09-15
  • 3 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信