第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用curl PHP 獲取API

使用curl PHP 獲取API

PHP
吃雞游戲 2023-09-15 10:16:56
我想從那時起獲取 APIhttps://www.travel-advisory.info/api我已經(jīng)編寫了我的代碼  $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 響應(yīng)的示例  "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貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個贊

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

print_r($this->continentFilter($data, 'AS'));

該函數(shù)將類似于:


private function continentFilter(array $data, string $continent): array

{

    $filteredArray = [];

    foreach ($data as $item) {

        if ($item['continent'] === $continent) {

            $filteredArray = $item;


            break;

        }

    }


    return $filteredArray;

}



查看完整回答
反對 回復(fù) 2023-09-15
?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個贊

您可以使用json_decode數(shù)組標(biāo)志和array_filter方法,如下所示:


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


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

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

});

另請注意,上面示例中給出的 json 需要全部用大括號括起來,如下所示:


{

  "data": {

  ...

}


查看完整回答
反對 回復(fù) 2023-09-15
?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個贊

如果大陸值與“AS”匹配,您可以循環(huán)遍歷每個對象并推送到新數(shù)組。


使用$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

            )


    )

    ...


查看完整回答
反對 回復(fù) 2023-09-15
  • 3 回答
  • 0 關(guān)注
  • 169 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號