Jump to content

What is wrong with my JSON results?


Recommended Posts

Ok, this may be dumb, but I'm really struggling with processing the data from a JSON result from the API with PHP.

 

Using exactly the official example on my tests:

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

 

 

 

I cannot access any results with PHP.

 

 

The results are there and I get the correct information with the API call, as I can see the data with:

var_dump($jsonData);

 

 

I'm not sure why, but I get a nicer output using instead:

print_r($jsonData);

 

 

Either way, my problem is that I cannot access any variable from PHP or turn them into variables.

 

 

No output. An example I tried using:

$domain_one = $jsonData[0]["domainname"];

 

 

Nothing.

 

 

I tried multiple variations, and I can't get anything. The WHMCS documentation does not explain how to convert them into variables either with PHP but this is supposed to work with JSON arrays, 0 is the first one, 1, is the second and so on. My problem is not with getting the data. The JSON data is there and the API works fine. It is how to process or use the data after getting the results.

Link to comment
Share on other sites

Ok, this may be dumb, but I'm really struggling with processing the data from a JSON result from the API with PHP.

 

Using exactly the official example on my tests:

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

 

 

 

I cannot access any results with PHP.

 

 

The results are there and I get the correct information with the API call, as I can see the data with:

var_dump($jsonData);

 

 

I'm not sure why, but I get a nicer output using instead:

print_r($jsonData);

 

 

Either way, my problem is that I cannot access any variable from PHP or turn them into variables.

 

 

No output. An example I tried using:

$domain_one = $jsonData[0]["domainname"];

 

 

Nothing.

 

 

I tried multiple variations, and I can't get anything. The WHMCS documentation does not explain how to convert them into variables either with PHP but this is supposed to work with JSON arrays, 0 is the first one, 1, is the second and so on. My problem is not with getting the data. The JSON data is there and the API works fine. It is how to process or use the data after getting the results.

 

Are you use local API or CURL ?

 

An example local for WHMCS 7.2+;

 

<?php
$command = 'GetClientsDomains';
$postData = array(
   'clientid' => '1',
   'stats' => true,
);

$results = localAPI($command, $postData);
$result = json_decode($results,true);

var_dump($result); // dump all results
if($result["result"] == "success") 
$firstDomainName = $result["domains"]["example.com"][0]["domainname"];
echo $firstDomainName;

 

its working.

Link to comment
Share on other sites

Are you use local API or CURL ?

 

An example local for WHMCS 7.2+;

 

<?php
$command = 'GetClientsDomains';
$postData = array(
   'clientid' => '1',
   'stats' => true,
);

$results = localAPI($command, $postData);
$result = json_decode($results,true);

var_dump($result); // dump all results
if($result["result"] == "success") 
   $firstDomainName = $result["domains"]["example.com"][0]["domainname"];
   echo $firstDomainName;

 

its working.

 

CURL. Exactly as the WHMCS example above from the posted link.

 

You do realize in your code you are inputting the domain name, right?

 

If you have to guess the domain name, or you already know the domain name, then you don't need the API call in the first place. How are you going to know the domain name from a customer when you are pulling it from the API? Every customer would show different domains which you don't know upfront until you get the results.

Link to comment
Share on other sites

Ok, this may be dumb, but I'm really struggling with processing the data from a JSON result from the API with PHP.

 

Using exactly the official example on my tests:

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

 

 

 

I cannot access any results with PHP.

 

 

The results are there and I get the correct information with the API call, as I can see the data with:

var_dump($jsonData);

 

 

I'm not sure why, but I get a nicer output using instead:

print_r($jsonData);

 

 

Either way, my problem is that I cannot access any variable from PHP or turn them into variables.

 

 

No output. An example I tried using:

$domain_one = $jsonData[0]["domainname"];

 

 

Nothing.

 

 

I tried multiple variations, and I can't get anything. The WHMCS documentation does not explain how to convert them into variables either with PHP but this is supposed to work with JSON arrays, 0 is the first one, 1, is the second and so on. My problem is not with getting the data. The JSON data is there and the API works fine. It is how to process or use the data after getting the results.

 

Have you got the following line in your cURL request?

 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

Just an FYI, I use this function to run all my cURL's for my WHMCS install, the $whmcsArray has all the postfields and gets called as needed.

 

function callWHMCSAPI($whmcsArray)
{
   global $whmcsUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcsUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($whmcsArray));
$returnData = curl_exec($ch);
curl_close($ch);
return json_decode($returnData,true);
}

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