【问题标题】:How to use the JSON data and prepare PHP array如何使用 JSON 数据并准备 PHP 数组
【发布时间】:2020-04-23 17:21:23
【问题描述】:

您好,我正在使用一个 API,它为我提供 JSON 数据,我想像这样在 php 中得到一个数组。

下面的代码为我提供了 JSON 格式的国家/地区列表

 <?php
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.dingconnect.com/api/V1/GetCountries",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "api_key: 5sIkXhACG4n5kaktzVECSl",
    "Cookie: visid_incap_1373684=W9x+2eX9SnqTQX976o3oKHP6p14AAAAAQUIPAAAAAACMqfJ0lIy4A94PIJ9/k566; visid_incap_1694192=CGL0RcqDS4y8rtfUDyl8RIn7p14AAAAAQUIPAAAAAAAgAZUccrSXJcreVE5ZcnLv; incap_ses_314_1694192=sszNShN26mgsFiNoe41bBOCRrV4AAAAAIX3OEAYdaCajfehID6IgsQ=="
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

收到的示例 JSON 数据

{
    "ResultCode": 1,
    "ErrorCodes": [],
    "Items": [
        {
            "CountryIso": "AD",
            "CountryName": "Andorra",
            "InternationalDialingInformation": [],
            "RegionCodes": [
                "AD"
            ]
        },
        {
            "CountryIso": "AE",
            "CountryName": "United Arab Emirates",
            "InternationalDialingInformation": [
                {
                    "Prefix": "971",
                    "MinimumLength": 12,
                    "MaximumLength": 12
                }
            ],
            "RegionCodes": [
                "AE"
            ]
        },
        {
            "CountryIso": "AF",
            "CountryName": "Afghanistan",
            "InternationalDialingInformation": [
                {
                    "Prefix": "93",
                    "MinimumLength": 11,
                    "MaximumLength": 11
                }
            ],
            "RegionCodes": [
                "AF"
            ]
        },
        {
            "CountryIso": "AG",
            "CountryName": "Antigua",
            "InternationalDialingInformation": [
                {
                    "Prefix": "1268",
                    "MinimumLength": 11,
                    "MaximumLength": 11
                }
            ],
            "RegionCodes": [
                "AG"
            ]
        },
        {
            "CountryIso": "AI",
            "CountryName": "Anguilla",
            "InternationalDialingInformation": [
                {
                    "Prefix": "1264",
                    "MinimumLength": 11,
                    "MaximumLength": 11
                }
            ],
            "RegionCodes": [
                "AI"
            ]
        },
        {
            "CountryIso": "AL",
            "CountryName": "Albania",
            "InternationalDialingInformation": [
                {
                    "Prefix": "355",
                    "MinimumLength": 12,
                    "MaximumLength": 12
                }
            ],
            "RegionCodes": [
                "AL"
            ]
        },
        {
            "CountryIso": "AM",
            "CountryName": "Armenia",
            "InternationalDialingInformation": [
                {
                    "Prefix": "374",
                    "MinimumLength": 11,
                    "MaximumLength": 11
                }
            ],
            "RegionCodes": [
                "AM"
            ]
        },
        {
            "CountryIso": "AN",
            "CountryName": "Netherlands Antilles",
            "InternationalDialingInformation": [
                {
                    "Prefix": "599",
                    "MinimumLength": 10,
                    "MaximumLength": 10
                }
            ],
            "RegionCodes": [
                "AN"
            ]
        },
        {
            "CountryIso": "AO",
            "CountryName": "Angola",
            "InternationalDialingInformation": [
                {
                    "Prefix": "244",
                    "MinimumLength": 12,
                    "MaximumLength": 12
                }
            ],
            "RegionCodes": [
                "AO"
            ]
        },
        {
            "CountryIso": "AR",
            "CountryName": "Argentina",
            "InternationalDialingInformation": [
                {
                    "Prefix": "54",
                    "MinimumLength": 12,
                    "MaximumLength": 12
                }
            ],
            "RegionCodes": [
                "AR"
            ]
        },
        {
            "CountryIso": "AS",
            "CountryName": "American Samoa",
            "InternationalDialingInformation": [
                {
                    "Prefix": "1684",
                    "MinimumLength": 11,
                    "MaximumLength": 11
                }
            ],
            "RegionCodes": [
                "AS"
            ]
        },
        {
            "CountryIso": "AT",

我想要的是有一个国家名称和 ID 的数组,这样我就可以下拉接收到的国家列表。

我真的不知道该怎么做。 非常感谢任何帮助。 谢谢x

【问题讨论】:

  • 我很抱歉地说不,我无法理解答案,而且问题也不同,他有我遇到的问题,但我也不确定我是否在正确的方向获取数据与否!但非常感谢您的评论。
  • 确实是同一个问题。如果您使用的 API 未设置 CORS 标头,则无法从浏览器使用 API,需要使用其中一种解决方法。我看到您的具体问题可能每周出现一次,但答案总是或多或少相同。使用您控制的服务器端代理或让 API 所有者添加 CORS 标头。
  • 也请看这里:stackoverflow.com/….
  • 没有 REST Level 0 Web 服务。理查森多数模型 (RMM) 是无稽之谈,即使有了第 3 级,人们也可能拥有违反 REST 架构原则的服务器或客户端。 IE。 RMM 甚至不谈论媒体类型支持和内容类型协商。

标签: php json rest api


【解决方案1】:

如果 $response 是 JSON,您可以执行 json_decode($response); 将 JSON 转换为 PHP 对象或数组,然后您可以从那里提取国家/地区的名称和 ID。

【讨论】:

    【解决方案2】:

    您需要json_decode $response,循环读取每个项目并将其传递给新创建的数组。

    注意:如果您不需要 ISO 代码,也可以使用 array_map 代替 foreach

    $response = curl_exec($curl);  // original line
    
    $countryData = json_decode($response)->Items;
    
    $countries = [];
    if ($countryData !== false) {
        foreach ($countryData as $countryItem) {
            $countries[$countryItem->CountryIso] = $countryItem->CountryName;
        }
    }
    
    // var_dump($countries);
    

    您的$countries 将是一个数组:

    [
        "AD" => "Andorra",
        "AE" => "United Arab Emirates",
        "AF" => "Afghanistan",
        // etc.
    ]
    

    【讨论】:

      【解决方案3】:

      我建议您使用jQuery 包或CDN。发出 API 请求时非常棒,并且对 Ajax 提供了很好的支持。我个人发现使用AjaxjQuery 会更好,因为它更简单。

      【讨论】:

        猜你喜欢
        • 2021-05-17
        • 2018-04-13
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        • 2015-08-01
        • 1970-01-01
        • 2013-08-01
        • 1970-01-01
        相关资源
        最近更新 更多