yggdrasil Posted August 2, 2017 Share Posted August 2, 2017 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 0 Quote Link to comment Share on other sites More sharing options...
yggdrasil Posted August 7, 2017 Author Share Posted August 7, 2017 No love 0 Quote Link to comment Share on other sites More sharing options...
sentq Posted August 7, 2017 Share Posted August 7, 2017 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; 0 Quote Link to comment Share on other sites More sharing options...
yggdrasil Posted August 9, 2017 Author Share Posted August 9, 2017 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; 0 Quote Link to comment Share on other sites More sharing options...
sentq Posted August 10, 2017 Share Posted August 10, 2017 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); 0 Quote Link to comment Share on other sites More sharing options...
yggdrasil Posted August 10, 2017 Author Share Posted August 10, 2017 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 {...} 0 Quote Link to comment Share on other sites More sharing options...
sentq Posted August 10, 2017 Share Posted August 10, 2017 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']; 0 Quote Link to comment Share on other sites More sharing options...
yggdrasil Posted August 10, 2017 Author Share Posted August 10, 2017 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', ) ) ); 0 Quote Link to comment Share on other sites More sharing options...
yggdrasil Posted August 10, 2017 Author Share Posted August 10, 2017 (edited) 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 August 10, 2017 by yggdrasil 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.