Question

I'm using WHM/cPanel's API to create accounts for users via PHP. I've got all the cURL options working to send the correct URL string to the server etc and the accounts are made properly so that side of things is all working fine.

What I need to do now is be able to print out a proper response message, rather than it just dumping all the data to the screen, as this is intended for use by general users who won't need to see all the nameserver, DNS etc. information that is done in the background.

The JSON result has a statusmsg key in the outputted array, but I'm not sure how to print it out.

This is the result with an error:

$result = json_decode($curl_response, true);

(From a var_dump)

object(stdClass)#1 (1) {
    ["result"]=> array(1) {
        [0]=> object(stdClass)#2 (4) {
            ["options"]=> NULL
            ["rawout"]=> NULL
            ["statusmsg"]=> string(55) "Sorry, a passwd entry for that username already exists."
            ["status"]=> int(0)
        }
    }
}

I've tried to do echo $result->result->statusmsg; but it just comes up with a blank page, so I'm not sure what I'm doing wrong or if there's another way to access the status message to I can print it the the users in a more friendly way.

Any help would be grand!

Was it helpful?

Solution 2

I realise I'm talking to myself now, but I've figured it out and am posting it in case others find this topic:

The result is a multidimensional array, so you need to get through all the array's to get the key you want - in this case the status message.

To get the array key statusmsg from the array:

Array ( [result] => Array (
        [0] => Array (
            [statusmsg] => Sorry, that username (testingw) is reserved.
            [status] => 0
            [rawout] =>
            [options] =>
        )
    )
)

You need to echo out: $result[result][0]['statusmsg']

You can then also do more error checking with the status key, as it will be 1 or 0 for pass or fail, respectively.

*(Thanks to Mike above for getting me on the right track with the json_decode array flag.)*

OTHER TIPS

json_decode works in two ways Array output and Object output. By adding ,true); to the end you have enabled Array output so to access this you would use.

$result['result']['statusmsg']

If you only used $result = json_decode($curl_response); then it would be an object and you can access it by using

$result->result->statusmsg

I also recommend outputting JSON with echo $curl_response; so that you can see its structure alot easier. If your browser doesn't prettify it then install something like JSONView for Chrome.

UPDATE:

Due to the fact the result is an array of objects you will need to loop through result to get each status message something like the following will accomplish that

foreach($result->result as $key=>$value) {
    echo $value->statusmsg;
}

or for the array output you can use

foreach($result['result'] as $key=>$value) {
    echo $value['statusmsg'];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top