0

I am trying to use fiken.no`s API to retrieve customernumbers. The API is HAL+JSON based and I want to access the data with PHP.

When creating a new account you receive no response, but only HTTP 201, so to get the generated customernumber I need to use the search endpoint.

That endpoint only returns all users like this:

{
"_links": {
    "self": {
        "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts"
    }
},
"_embedded": {
    "https://fiken.no/api/v1/rel/contacts": [
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/757941482"
                }
            },
            "name": "Ola Nordmann 2",
            "email": "mail@mail.com",
            "address": {
                "country": "Norge"
            },
            "customerNumber": 10003
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/757941171"
                }
            },
            "name": "Ola Nordmann 1",
            "email": "findthis@example.com",
            "address": {
                "country": "Norge"
            },
            "customerNumber": 10002
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/756867201"
                }
            },
            "name": "Demoleverandør",
            "address": {
                "address1": "Demoveien 44",
                "address2": "",
                "postalPlace": "Oslo",
                "postalCode": "0190",
                "country": "Norge"
            },
            "supplierNumber": 20001
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/756867200"
                }
            },
            "name": "Demokunde",
            "address": {
                "address1": "Demoveien 22",
                "address2": "",
                "postalPlace": "Oslo",
                "postalCode": "0190",
                "country": "Norge"
            },
            "customerNumber": 10001
        }
    ]
}}

From this response I need to query for f.ex the email findthis@example.com and get the whole user object from that. That includes the address data and especially the customernumber. How would I do that?

I have found this: Search a key in a Json (nested array) PHP that looks like my issues but here the key is constant in the JSON array. Here it is from 0 and out to infinity.

Is there any better way to process this using PHP than normal JSON practices?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

First thing would be to decode the JSON into an array:

$resultArray = json_decode($yourJSONvariable, true);

Next I would recommend isolating the array of contacts from the JSON array:

$contacts = $resultArray["_embedded"]["https://fiken.no/api/v1/rel/contacts"];

Now $contacts should be an array of all the contacts from the API call. You can loop through each one to find the record with the matching email like this:

foreach ($contacts as $contact) {
    if ($contact["email"] == "findthis@example.com") {
        $mycontact = $contact;
        break;
    }
}

$mycontact will now contain an array of the contact with the matching email, and you can access its individual fields using the field names as the array index (e.g., $mycontact["name"], $mycontact["address"]["country"], $mycontact["_links"]["self"]["href"]). To see all data in the array do var_dump($contacts)

tshimkus
  • 1,173
  • 2
  • 17
  • 24
  • Thank you for the answer. I had thought about using foreach but was wondering if it was recommended or not. But now i know. Thanks for the help buddy. Btw. Edited your post since when you referenced the _embedded json key you had accidentally missed the underscore – Oscar Sortland Kolsrud Feb 01 '19 at 23:22
  • 1
    I have yet to find a better way to search through JSON and return a specific record. A more ideal solution would be if you could query the contacts endpoint with an email parameter, but I skimmed over the documentation and it does not appear to be possible. – tshimkus Feb 01 '19 at 23:32
  • Nope, Fiken is a startup company so i fully understand that their API is a WIP. Thank you so much for helping me out on this and even taking the time to skim through the fiken api docs eventhough i forgot to link them – Oscar Sortland Kolsrud Feb 01 '19 at 23:37