0

Really need some help with the following.

So, my script calls an API on remote server with:

    try {
    $api = new ApiClient();
    $api->call('GetNameservers', $postfields);

    return array(
        'success' => true,
        'ns1' => $api->getFromResponse('nameserver1'),
        'ns2' => $api->getFromResponse('nameserver2'),
        'ns3' => $api->getFromResponse('nameserver3'),
        'ns4' => $api->getFromResponse('nameserver4'),
        'ns5' => $api->getFromResponse('nameserver5'),
    );

}

The above API call correctly returns what is must:

Array
(
[result] => 1000
[resData] => Array
    (
        [domain] => somecooldomain.com
        [status] => Active
        [autorenew] => 0
        [contactid] => 822
        [nameserver1] => ns1.nameserver1.com
        [nameserver2] => ns2.nameserver2.com
        [nameserver3] => ns3.nameserver3.com
        [nameserver4] => ns4.nameserver4.com
        [nameserver5] => 
        [registrationdate] => 2018-06-29
        [expirydate] => 2020-11-03
    )
)

My problem comes after:

return array(
    'success' => true,

This works:

'ns1' => $api->getFromResponse('result'),

But it is not what I need as the array is nested. I've tried many things here:

'ns1' => $api->getFromResponse('resData.nameserver1'),
'ns1' => $api->getFromResponse(['resData']['nameserver1']),

The getFromReponse is as follow:

    public function getFromResponse($key)
{
    return isset($this->results[$key]) ? $this->results[$key] : '';
}
4
  • have you tried this $api['resData']['nameserver1']? Commented Jul 28, 2018 at 6:09
  • I tried this, unfortunately, did not work. Commented Jul 28, 2018 at 7:13
  • 1
    debug: var_dump($api->getFromResponse('result'), $api->getFromResponse('result')['resData']['nameserver1']); The first one will show the full structure. The second should return the expected entry from that structure Commented Jul 28, 2018 at 8:27
  • Hi Ryan, I just want to confirm the syntax here. Doing 'ns1' => $api->getFromResponse('result')['resData']['nameserver1'], actually returns string(4) "1000" string(1) "1" Commented Jul 28, 2018 at 8:46

1 Answer 1

2

The API response returns a multidimensional array, but your function public function getFromResponse($key) only has provisions for 1 dimension.

The return value of $api->getFromResponse('resData') is actually an array, so it is valid to do something like this: $api->getFromResponse('resData')['nameserver1'] to get the value of items from the inner array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.