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.

cPanel API for 2082

Discussion in 'cPanel Developers' started by vijayv2205, Apr 18, 2014.

  1. vijayv2205

    vijayv2205 Registered

    Joined:
    Apr 18, 2014
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello,

    I have tried the https://github.com/CpanelInc/xmlapi-php.

    I am able to get all account list via WHM(2087 port).

    But I am unable to get list of all domain in side a particular account via 2082 port.

    Please help!
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,723
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Could you elaborate on how you are attempting to obtain this list? Are you seeking to have parked/addon/subdomains of an account listed?

    Thank you.
     
  3. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    Hello,
    You can only call listaccts from WHM, not cPanel. All these calls are only available via 2087:

    XML and JSON APIs

    If you want to get the list of domains within an account as a user, unfortunately, you have to call multiple API2 calls like so to get parked,addon,and main domains, then another to get subdomains:

    Code:
    $account = "username";
    $xmlapi->set_port(2083);
    $parked_addon_main_domains = $xmlapi->api2_query($account, "DomainLookup", "getbasedomains" );
    $sub_domains = $xmlapi->api2_query($account, "SubDomain", "listsubdomains" );
    
    Unfortunately, this will also give you subdomains that are actually just the subdomain aliases for parked or addon domains. So, you'll actually want to do something like this:

    Code:
    <?php
    
    include 'xmlapi.php';
    
    #Set these 3 variables to your server and account info
    $ip = "";
    $cpanel_username = "";
    $cpanel_password = "";
    	
    
    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth($cpanel_username,$cpanel_password);
    $xmlapi->set_output("json");
    $xmlapi->set_port(2083);
    $xmlapi->set_debug(1);
    
    $domains = get_all_cpanel_domains($xmlapi, $cpanel_username);
    echo "DOMAINS:\n";
    print_r($domains);
    
    function get_all_cpanel_domains($xmlapi, $username){
    	$all_sites_without_subdomains_that_are_actually_parked_or_addons = array();
    	$main_domain = get_primary_domain($xmlapi, $username);
    	$document_roots = get_document_roots($xmlapi, $username);
    	
    	foreach ( $document_roots as $doc_root ) {
    	    $addon = 0;
    		$matches = array();
    		#If the domain name is something.$mainDomain, it's either a sub or addon sub
    		if( preg_match("/([a-zA-Z0-9\-]+)\.$main_domain/", $doc_root->domain, $matches ) ){
    			#now if the first part of the domain name is the same as the folder name, it's a subdomain, not an addon
    			if( preg_match( "/.+$matches[0]$/", $doc_root->docRoot)) {
    				$addon = 1;
    			}
    		}
    	    if ( !$addon ) { 
    			array_push( $all_sites_without_subdomains_that_are_actually_parked_or_addons, $doc_root );
    			#If you want just the domain name, and not also the doc root, comment out the line above
    			#and uncomment the line below: 
    			#array_push( $all_sites_without_subdomains_that_are_actually_parked_or_addons, $doc_root->domain );
    		}
    	}
    	
    	return $all_sites_without_subdomains_that_are_actually_parked_or_addons;
    }
    
    function get_primary_domain($xmlapi, $username){
    	$json = $xmlapi->api2_query($username, "DomainLookup", "getmaindomain" );
    	$result = json_decode($json);
    	var_dump($result); #Debug info
    	$main_domain = $result->cpanelresult->data[0]->main_domain;
    	return $main_domain;
    }
    
    function get_document_roots($xmlapi, $username){
    	$json = $xmlapi->api2_query($username, "DomainLookup", "getdocroots" );
    	$result = json_decode($json);
    	var_dump($result); #Debug info
    	$document_roots = $result->cpanelresult->data;
    	return $document_roots;
    }
    
    It's also possible that you may not want to include domains that are redirected to another domain in your results. You'd have to do a similar filter against Mime::listredirects: Mime Module Documentation
     
Loading...

Share This Page