I have been working on an XMLAPI PHP class for awhile now, this is intended completely as reference code and is not supported by cPanel. Feel free to modify this and reuse this code as needed.
I want to start out by stating that I am not a PHP programmer, my background is mostly in working with perl, so if you see any stupid developer errors, please let me know and I will gladly adjust them. If you find any bugs, please report them in this thread or by PM'ing me (we will work out better communication methods later on). When reporting bugs, please state the version number.
This class is written for PHP5 and relies on curl & simplexml to work correctly. This include file should only be used as a class.
A basic explanation of how to use this should be obvious from the example scripts attached, however I will go over what this is capable of:
All xml-api functions are implemented in this, provided with an easy interface:
f.ex:
Some functions are called with normal arguments:
f.ex:
On top of this, some functions require associative arrays (hashes) to be passed to them:
When instantiating this class it needs to call both the constructor and an authenticator function. This class support both password authentication and hash authentication, called via password_auth(user,pass) and hash_auth(user,authhash) respectively.
f.ex with hash auth.
f.ex. with password auth.
Another feature is that this object can generate the API1 and API2 code automatically:
f.ex.
Along with this, some debugging functions are provided with a simple boolean interface.
The first of these is set_debug which will show all URLs generated, API1/API2 XML and responses in XML and SimpleXML formats:
f.ex. call
will result in an output similar to:
There is also a return_xml object that will just return the XML as a string rather than a SimpleXML object.
Edit: This has been re-released under a modified BSD license, please see the link below to download this version (there have been no changes in this library, a new version is coming after 11.25 is released)
http://sdk.cpanel.net/lib/cp_xmlapi_php.tar.gz
I want to start out by stating that I am not a PHP programmer, my background is mostly in working with perl, so if you see any stupid developer errors, please let me know and I will gladly adjust them. If you find any bugs, please report them in this thread or by PM'ing me (we will work out better communication methods later on). When reporting bugs, please state the version number.
This class is written for PHP5 and relies on curl & simplexml to work correctly. This include file should only be used as a class.
A basic explanation of how to use this should be obvious from the example scripts attached, however I will go over what this is capable of:
All xml-api functions are implemented in this, provided with an easy interface:
f.ex:
Code:
$xmlapi->listaccts() can be used to list accounts
f.ex:
Code:
$xmlapi->addip($ip,$netmask)
Code:
$acct = array( username => "someuser", password => "pass123", domain => "thisdomain.com");
$xmlapi->createacct($acct);
When instantiating this class it needs to call both the constructor and an authenticator function. This class support both password authentication and hash authentication, called via password_auth(user,pass) and hash_auth(user,authhash) respectively.
f.ex with hash auth.
Code:
$xmlapi = new xmlapi($ip);
$xmlapi->hash_auth("root",$root_hash);
Code:
$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("root",$root_pass);
f.ex.
Code:
print $xmlapi->api1_query($account, "Email", "addpop", array($email_user, $email_password, $email_domain) );
The first of these is set_debug which will show all URLs generated, API1/API2 XML and responses in XML and SimpleXML formats:
f.ex. call
Code:
$xmlapi->set_debug(1);
$xmlapi->listips();
Code:
QUERY:
https://127.0.0.1:2087/xml-api/listips?
RAW XML:
<listips>
<result>
<active>1</active>
<if>eth1</if>
<ip>127.0.0.1</ip>
<mainaddr>1</mainaddr>
<removable>0</removable>
<used>1</used>
</result>
<result>
<active>1</active>
<if>eth1:1</if>
<ip>127.0.0.2</ip>
<mainaddr>0</mainaddr>
<removable>0</removable>
<used>1</used>
</result>
<result>
<active>1</active>
<if>eth1:3</if>
<ip>127.0.0.3</ip>
<mainaddr>0</mainaddr>
<removable>1</removable>
<used>0</used>
</result>
<result>
<active>1</active>
<if>eth1:4</if>
<ip>127.0.0.4</ip>
<mainaddr>0</mainaddr>
<removable>1</removable>
<used>0</used>
</result>
</listips>
<!-- Web Host Manager (c) cPanel, Inc. 2008 http://cpanel.net/ Unauthorized copying is prohibited. -->
object(SimpleXMLElement)#2 (1) {
["result"]=>
array(4) {
[0]=>
object(SimpleXMLElement)#3 (6) {
["active"]=>
string(1) "1"
["if"]=>
string(4) "eth1"
["ip"]=>
string(14) "127.0.0.1"
["mainaddr"]=>
string(1) "1"
["removable"]=>
string(1) "0"
["used"]=>
string(1) "1"
}
[1]=>
object(SimpleXMLElement)#4 (6) {
["active"]=>
string(1) "1"
["if"]=>
string(6) "eth1:1"
["ip"]=>
string(14) "127.0.0.2"
["mainaddr"]=>
string(1) "0"
["removable"]=>
string(1) "0"
["used"]=>
string(1) "1"
}
[2]=>
object(SimpleXMLElement)#5 (6) {
["active"]=>
string(1) "1"
["if"]=>
string(6) "eth1:3"
["ip"]=>
string(14) "127.0.0.3"
["mainaddr"]=>
string(1) "0"
["removable"]=>
string(1) "1"
["used"]=>
string(1) "0"
}
[3]=>
object(SimpleXMLElement)#6 (6) {
["active"]=>
string(1) "1"
["if"]=>
string(6) "eth1:4"
["ip"]=>
string(14) "127.0.0.4"
["mainaddr"]=>
string(1) "0"
["removable"]=>
string(1) "1"
["used"]=>
string(1) "0"
}
}
}
Edit: This has been re-released under a modified BSD license, please see the link below to download this version (there have been no changes in this library, a new version is coming after 11.25 is released)
http://sdk.cpanel.net/lib/cp_xmlapi_php.tar.gz
Last edited: