The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

SOLVED Manage Json Result from API

Discussion in 'cPanel Developers' started by diracuser, Aug 8, 2017.

Tags:
  1. diracuser

    diracuser Member

    Joined:
    Oct 22, 2014
    Messages:
    21
    Likes Received:
    1
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi
    I'm making a script to generate email accounts.
    I want to manage the json result, but all the time it returns NULL when I access to the fields "reason" and "result"
    This is my var_dump result from API:
    Code:
    vstring(232) {"cpanelresult":{"error":"The account userauto@xxxx.xxx already exists!","func":"addpop","data":[{"reason":"The account xxxo@xx.xxx already exists!","result":0}],"apiversion":2,"module":"Email","event":{"result":1}}}
    
    and this is my access to the fields that return NULL:
    Code:
    var_dump ($resultado['cpanelresult']['data'][0]['reason']);
    var_dump ($resultado['cpanelresult']['data'][0]['result']);
    
    How can I access?
     
    #1 diracuser, Aug 8, 2017
    Last edited by a moderator: Aug 9, 2017
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,064
    Likes Received:
    1,287
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
  3. diracuser

    diracuser Member

    Joined:
    Oct 22, 2014
    Messages:
    21
    Likes Received:
    1
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Thanks for your response.

    But I have the same problem.

    This is my script:


    Code:
    <?
    require_once "/usr/local/cpanel/php/cpanel.php";
    $cpanel = new CPANEL(); // Connect to cPanel - only do this once.
    
    // Call the function.
    $my_variable = $cpanel->uapi(
        'Email', 'add_pop',
        array(
            'email'           => 'xxxxx',
            'password'        => 'xxxx',
            'quota'           => '1024',
            'domain'          => 'xxx.xx',
            'skip_update_db'  => '1',
            )
    );
    
    
    var_dump (json_encode($my_variable));
    $resultado = json_encode($my_variable);
    var_dump($resultado[cpanelresult][result][errors]);
    echo($resultado[cpanelresult][result][errors]);
    
     ?>
    And this is the output:


    var_dump:
    Code:
    string(192) "{"cpanelresult":{"func":"add_pop","apiversion":3,"result":{"status":0,"data":null,"messages":null,"metadata":[],"errors":["The account xxx@xxxx.xxx already exists!"]},"module":"Email"}}"
    
    var_dump and for the field func:
    string(1) "{" 
    {
    
    echo returns: 
    {
    
     
    #3 diracuser, Aug 9, 2017
    Last edited by a moderator: Aug 9, 2017
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,064
    Likes Received:
    1,287
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
  5. diracuser

    diracuser Member

    Joined:
    Oct 22, 2014
    Messages:
    21
    Likes Received:
    1
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    My API works fine, because :

    string(192) "{"cpanelresult":{"func":"add_pop","apiversion":3,"result":{"status":0,"data":null,"messages":null,"metadata":[],"errors":["The account xxx@xxxx.xxx already exists!"]},"module":"Email"}}"

    Is the api result, and the API tells that the account already exist .It's ok because in my test this account exists....

    I resolve doing this:

    $resulCpanel =$cpanel->get_result();
    $resull0 = $resulCpanel[0]['result'];
    $resul1 = $resulCpanel[0]['reason'];




    Thanks
     
    cPanelMichael likes this.
Loading...

Share This Page