Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

PHP XML API - output in error log but not on screen!

Discussion in 'cPanel Developers' started by benwiggy, May 27, 2011.

  1. benwiggy

    benwiggy Member

    Jul 3, 2010
    Likes Received:
    Trophy Points:
    Hi guys, bit of a noob but with some difficultly I've managed to get to the following stage. The code receives the data but doesn't output it on screen - just to the error log - any help much appreciated.


    $ip $_SERVER['SERVER_ADDR'];

    $user "[obscured]";
    $password "[obscured]";

    $xmlapi = new xmlapi($ip);
    Page is blank but I get this output in the error log:

    [27-May-2011 11:14:00] URL: http://[obscured]:2082/xml-api/cpanel
    [27-May-2011 11:14:00] DATA: cpanel_xmlapi_user=[obscured]&cpanel_xmlapi_module=Email&cpanel_xmlapi_func=listpopswithdisk&cpanel_xmlapi_apiversion=2
    [27-May-2011 11:14:00] Authentication Header: Authorization: Basic [obscured]==
    [27-May-2011 11:14:00] RESPONSE:
     <?xml version="1.0" ?>
          <humandiskquota>250 MB</humandiskquota>
          <humandiskused>11.88 MB</humandiskused>
          <humandiskquota>50 MB</humandiskquota>
    [27-May-2011 11:14:00] SimpleXML var_dump:
    SimpleXMLElement Object
        [apiversion] => 2
        [data] => Array
                [0] => SimpleXMLElement Object
                        [_diskquota] => 262144000
                        [_diskused] => 12456556
                        [diskquota] => 250
                        [diskused] => 11.88
                        [diskusedpercent] => 4
                        [diskusedpercent20] => 0
                        [domain] => [obscured].com
                        [email] => admin@[obscured].com
                        [humandiskquota] => 250 MB
                        [humandiskused] => 11.88 MB
                        [login] => admin@[obscured].com
                        [mtime] => 1306509011
                        [txtdiskquota] => 250
                        [user] => admin
                [1] => SimpleXMLElement Object
                        [_diskquota] => 52428800
                        [_diskused] => 0
                        [diskquota] => 50
                        [diskused] => 0
                        [diskusedpercent] => 0
                        [diskusedpercent20] => 0
                        [domain] => [obscured].com
                        [email] => recruiting@[obscured].com
                        [humandiskquota] => 50 MB
                        [humandiskused] => None
                        [login] => recruiting@[obscured].com
                        [mtime] => 1306509011
                        [txtdiskquota] => 50
                        [user] => recruiting
        [event] => SimpleXMLElement Object
                [result] => 1
        [func] => listpopswithdisk
        [module] => Email
  2. cPanelDavidN

    cPanelDavidN Integration Developer

    Dec 17, 2009
    Likes Received:
    Trophy Points:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi benwiggy,

    I suspect that nothing is being render to screen (buffer/html page that is served) is because performing a 'print' as follows is technically an error:
    print $xmlapi->api2_query($user"Email""listpopswithdisk");  //likely will be an error
    By default, the PHP XML-API client class will return a SimpleXML object (which we know is getting created since it's in you debug output). But if we try to 'echo' or 'print' a non-scalar variable (ie, array, object, boolean, or resource) then we won't get it's stored value, but instead unexpected output (IIRC, 'echo' will print the variable type name; not sure about print).

    Anyway, you have several options.
    1) instead of 'print', you could assign it to a variable and iterate over the SimpleXML object, possibly with value tests, etc --this would be the most involved especially if you were not sure of the output structure or if you used a generic "render" function for all query methods, but this is what I see most people do.

    2) instead of 'print', you could assign it to a variable and use 'var_dump()' on the should render identically to the SimpleXML structure in the debug output. -- really, this is just good for verifying the query method returned usable data without you having to look as the error_log or change the error_log/display_errors settings in you script.

    3) use $xmlapi->set_output($my_output_type) where $my_output_type is either 'json', 'array', or 'xml'. This cause the output of the query methods to be modified as follows. 'json' will return a JSON string that you must parse yourself (likely with 'json_decode()', if you are PHP >= 5.2). 'array' will, obviously, return an associative array (with deeply nested, possibly ordinal or associative array, data). And 'xml' gives the SimpleXML object (the default).

    Give that a go, I think that should solve the blank page thing.

    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. benwiggy

    benwiggy Member

    Jul 3, 2010
    Likes Received:
    Trophy Points:
    Hi David,

    I'm hugely grateful for the reply - you're absolutely right and thank you for the detailed set of options.

    (2) helped me with debugging and (3) was just the ticket as far as interpreting the output is concerned.

    I found the print_r function very helpful when interpreting the data (returned as an array - using the set_output feature)

    Thank you very much again - I'm now on track.

    #3 benwiggy, May 28, 2011
    Last edited: May 28, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice