Jump to content

How to convert JSON API results into vars?


yggdrasil

Recommended Posts

Hi, I'm testing this API call:

 

https://developers.whmcs.com/api-reference/domaingetnameservers/

 

That outputs the results in JSON like this:

{

"result": "success",

"ns1": "ns1.example.com",

"ns2": "ns1.example.com"

}

 

But how do I go by using each individual result instead of the whole string? I don't want to print the full result with PHP and of course that is rather useless as I need to compare the individual nameservers or at least be able to tap into the result itself, and not the whole data.

 

Ideally, I would like to convert each result into a PHP var like:

$ns1 = JSON ns1 result

$ns2 = JSON ns2 result

 

And so on. What would be cleanest way to accomplish this? I tried multiple codes related JSON and arrays but none seem to work :(

Link to comment
Share on other sites

one possible way is to define the array keys as vars

$json = json_decode('{"result": "success","ns1": "ns1.example.com","ns2": "ns1.example.com"}', true);
foreach ($json as $key => $val){
   $$key = $val;
}

echo $ns1;

 

This is sadly not working for me.

 

$response = curl_exec($ch);
if (curl_error($ch)) {    
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);

// Decode response
$json = json_decode($response, true);
foreach ($json as $key => $val){
$$key = $val;
}
echo $ns1;

Link to comment
Share on other sites

This is sadly not working for me.

 

$response = curl_exec($ch);
if (curl_error($ch)) {    
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);

// Decode response
$json = json_decode($response, true);
foreach ($json as $key => $val){
$$key = $val;
}
echo $ns1;

does $response return any result?

 

try var_dump($response);

Link to comment
Share on other sites

Yes, it responds true. The strange thing is that I see results by just using:

$response = curl_exec($ch);

 

Everything after that line seems redundant. I get full JSON results by using just that. If I comment it out:

//$response = curl_exec($ch);

 

No results what so ever on the browser (blank).

 

The strange thing is that if I manually echo $response I just get 1. If I use your var_dump, I get true, but I suspect that is the same 1 or true just in a different format.

 

So it seems $response is only showing the successful state (1 = true), and not the actual data results. Of course, I don't want the full results displayed to the browser either; I want them in a variable, then try to get them individually and only echo if I want something or need it.

 

 

 

The results I see the browser by using:

$response = curl_exec($ch);

 

Are like this:

{"result":"success","ns1":"ns1.example.com","ns2":"ns2.example.com","ns3":null,"ns4":null,"ns5":null,"error":null}

 

I'm basing myself on the original WHMCS examples from the docs file. By the way in your example you echo $ns1, I assumed that was a mistake since there is no $ns1 var defined anywhere in your code, but I tried as well as well using $val or $key and nothing either. Using $json outputs just 1 as well. It seems $json = json_decode($response, true); actually gets 1 and not the data in the {...}

Link to comment
Share on other sites

this line need to be called otherwise your cURL will not be called.

 

better you add this line:

var_dump($response);

 

after:

$response = curl_exec($ch);

 

it will tell you exactly what values returned by calling this cURL request.

 

if the returned result is Array you can get the "ns1" value:

echo $response['ns1'];

 

if the returned result is in JSON format, you will need to convert this JSON into Array, then call it as shown above:

$response = curl_exec($ch);

$response = json_decode($response, true);

 

echo $response['ns1'];

Link to comment
Share on other sites

I added var_dump($response); right after the $response=curl_exec($ch); line and this is what I get:

 

{"result":"success","ns1":"ns1.example.com","ns2":"ns2.example.com","ns3":null,"ns4":null,"ns5":null,"error":null}bool(true)

 

 

So the var_dump only seems to add bool(true) to the result and nothing more.

 

 

The result is by default in JSON as far as I see from the code above it:

 

 

curl_setopt($ch, CURLOPT_POSTFIELDS,

http_build_query(

array(

'action' => 'DomainGetNameservers',

// See https://developers.whmcs.com/api/authentication

'username' => $username,

'password' => md5($password),

'domainid' => '1',

'responsetype' => 'json',

)

)

);

Link to comment
Share on other sites

Ok, I rewrote the whole CURL call and now I get this with

var_dump($jsonData);

 

array(7) { ["result"]=> string(7) "success" ["ns1"]=> string(17) "ns1.example.com" ["ns2"]=> string(17) "ns2.example.com" ["ns3"]=> NULL ["ns4"]=> NULL ["ns5"]=> NULL ["error"]=> NULL }

 

Is seems there is an error in the WHMCS example here:

https://developers.whmcs.com/api/sample-code/

So that example was not working for me before. The $password has to be passed as md5 in order for the call to work.

 

Is this better? Now the result seems to be an array.

 

- - - Updated - - -

 

I confirm that now I can get the vars directly as you posted before with something like:

echo $jsonData['ns2'];

 

I'm curious what the difference is between getting the results like this in the array or as I posted before.

Edited by yggdrasil
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated