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

    Joined:
    Jul 3, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    51
    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.

    PHP:
    include($_SERVER['DOCUMENT_ROOT']."/includes/xmlapi.php");

    $ip $_SERVER['SERVER_ADDR'];

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

    $xmlapi = new xmlapi($ip);
    $xmlapi->set_port(2082);
    $xmlapi->password_auth($user$password);
    $xmlapi->set_debug(1);
    print 
    $xmlapi->api2_query($user"Email""listpopswithdisk");
    Page is blank but I get this output in the error log:

    Code:
    [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" ?>
      <cpanelresult>
        <apiversion>2</apiversion>
        <data>
          <_diskquota>262144000</_diskquota>
          <_diskused>12456556</_diskused>
          <diskquota>250</diskquota>
          <diskused>11.88</diskused>
          <diskusedpercent>4</diskusedpercent>
          <diskusedpercent20>0</diskusedpercent20>
          <domain>[obscured].com</domain>
          <email>admin@[obscured].com</email>
          <humandiskquota>250 MB</humandiskquota>
          <humandiskused>11.88 MB</humandiskused>
          <login>admin@[obscured].com</login>
          <mtime>1306509011</mtime>
          <txtdiskquota>250</txtdiskquota>
          <user>admin</user>
        </data>
        <data>
          <_diskquota>52428800</_diskquota>
          <_diskused>0</_diskused>
          <diskquota>50</diskquota>
          <diskused>0</diskused>
          <diskusedpercent>0</diskusedpercent>
          <diskusedpercent20>0</diskusedpercent20>
          <domain>[obscured].com</domain>
          <email>recruiting@[obscured].com</email>
          <humandiskquota>50 MB</humandiskquota>
          <humandiskused>None</humandiskused>
          <login>recruiting@[obscured].com</login>
          <mtime>1306509011</mtime>
          <txtdiskquota>50</txtdiskquota>
          <user>recruiting</user>
        </data>
        <event>
          <result>1</result>
        </event>
        <func>listpopswithdisk</func>
        <module>Email</module>
      </cpanelresult>
    
    [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
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    3
    Trophy Points:
    68
    Location:
    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:
    PHP:
    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 variable...it 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.

    Regards,
    -DavidN
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. benwiggy

    benwiggy Member

    Joined:
    Jul 3, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    51
    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.

    Best,
    Ben
     
    #3 benwiggy, May 28, 2011
    Last edited: May 28, 2011
Loading...

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