1

Hello i am working with an API which gives me JSON data and i want to get a array out of that in php like this.

The code below gives me a list of countries in 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;

sample JSON Data received

{
    "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",

what i want is to have a array of country name and id so that i can make a drop down of countries list received.

i really have no idea how to do that. any help is really appreciated. Thankx

  • Does this answer your question? [No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API](https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe) – Evert Apr 23 '20 at 17:23
  • i am sorry to say that no, i could not understand the answer and also the question is different, he has the problem which i am getting but i am also not sure if i am in the right direction of getting the data or not ! but really thank you for your comment . –  Apr 23 '20 at 17:31
  • It's really the same issue. If the API you are using does not set CORS headers, you cannot use the API from a browser and need to use one of the workarounds. I see your specific question appear perhaps once a week, but the answer is always more or less the same. Use a server-side proxy you control or get the API owner to add CORS headers. – Evert Apr 23 '20 at 17:40
  • 1
    Take a look here too: https://stackoverflow.com/search?q=blocked+by+CORS+policy%3A+No+%27Access-Control-Allow-Origin%27+header+is+present+on+the+requested+resource. – Evert Apr 23 '20 at 17:42
  • There is no such thing as *REST Level 0 web service*. The Richardsen Majority Model (RMM) is nonsense in the regard that even with Level 3 in place one can have a server or client that is violating REST architecture principles. I.e. RMM does not even talk about media-type support and content-type negotiation. – Roman Vottner Apr 23 '20 at 20:11

3 Answers3

1

If $response is a JSON, you could do json_decode($response); which will convert the JSON in a PHP object or array, and you can work from there on extracting the name and ID of countries.

Ron
  • 5,900
  • 2
  • 20
  • 30
0

You need to json_decode the $response, read each item in a loop and pass it to a newly created array.

Note: If you didn't need the ISO codes, you could also use array_map instead of the 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);

Your $countries will be an array:

[
    "AD" => "Andorra",
    "AE" => "United Arab Emirates",
    "AF" => "Afghanistan",
    // etc.
]
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
-1

I would suggest you using the jQuery package or CDN. It is great when making API requests and it comes with a great support for Ajax. I personally find that with Ajax and jQuery you will have a much nicer experience, as it is more simple.

georgekrax
  • 1,065
  • 1
  • 11
  • 22