Hi Matt,
I was missing: $xmlapi->set_output('array');
Everything is fine now.
Hi Matt,
I was missing: $xmlapi->set_output('array');
Everything is fine now.
Great class
Thanks alot
Last edited by mshannaq; 01-16-2010 at 05:31 AM.
I have a Reseller account (i.e. have access to WHM, reselling cpanel accounts) and I'm having trouble trying to understand how to use the API PHP Class.
What I want to accomplish is a customer login system where my customers log in and they see, among other things, a table featuring their Disk Used, Disk Limit, Bandwidth Used and Bandwidth Limit.
I'm fine with developing the login system etc... I can even get "accountsummary" to display the data of an account I need, however it displays ALL the account summary output (in SimpleXML) and I don't know how to limit it to certain data.
My PHP code is as follows:
and it displays this:Code:<?php include("xmlapi.php"); $ip = "localhost"; # The access has can be found on your server under WHM's "Setup remote access hash" section or at /root/.accesshash $root_hash = 'MY HASH CODE HERE'; $xmlapi = new xmlapi($ip); $xmlapi->hash_auth("MY WHM ACCOUNT USERNAME",$root_hash); $xmlapi->return_xml(1); $xmlapi->set_debug(1); $username = CpanelUsername; $xmlapi->accountsummary($username); print $xmlapi->accountsummary($username); ?>
How on earth do I display, for example, only the data within the <diskused></diskused> tags? I have search for hours on more than one occasion on how to do this, and the only thing I can find are replies to similar questions suggesting they read the cPanel API documentation or SimpleXML Documentation. I have gone through these as best as I can, but I still don't know the answer to my question. These documentations either don't answer my question, or I'm just not understanding it.Code:<accountsummary> <acct> <disklimit>200M</disklimit> <diskused>63M</diskused> <domain>domain.com</domain> <email>email@domain.com</email> <ip>IP</ip> <maxaddons>100</maxaddons> <maxftp>100</maxftp> <maxlst>100</maxlst> <maxparked>100</maxparked> <maxpop>100</maxpop> <maxsql>100</maxsql> <maxsub>100</maxsub> <owner>OWNER</owner> <partition>home</partition> <plan>PLAN</plan> <shell>/usr/local/cpanel/bin/noshell</shell> <startdate>STARTDATE</startdate> <suspended>0</suspended> <suspendreason>not suspended</suspendreason> <suspendtime></suspendtime> <theme>x3</theme> <unix_startdate>UNIX_STARTDATE</unix_startdate> <user>USER</user> </acct> <status>1</status> <statusmsg>Ok</statusmsg> </accountsummary>
I've tried things like:
andCode:$diskused = $xmlapi->accountsummary->acct->diskused; print $diskused;
but none of these work.Code:print $xmlapi->accountsummary($username)->diskused;
I'd really appreciate any help any of you may be able to offer.
Many thanks,
Andrew
It's quite simple actually. Here is a very basic example that echoes out the domain name of each account
Code:// get list of accounts $accounts = $xmlapi->listaccts(); // create SimpleXML object to loop through later $xml2 = new SimpleXMLElement($accounts); foreach ($xml2->acct as $account) { $mydomain = $account->domain; echo $mydomain; }
Two api2 functions that I've added to my version of the xmlapi class for those who are looking for a way to check "mxcheck" (remote, local, auto, backup etc) and to set it.
Code:// This API function fetches an mx list for the supplied domain and specific user public function listmxs($username, $args = null) { if ( (!isset($username)) || (!isset($args)) ) { error_log("stat requires that a username and options are passed to it"); return false; } return $this->api2_query($username, 'Email', 'listmxs', $args); } // This API function sets mx check (local, remote, backup) for a domain and user. public function setmxcheck($username, $args = null) { if ( (!isset($username)) || (!isset($args)) ) { error_log("stat requires that a username and options are passed to it"); return false; } return $this->api2_query($username, 'Email', 'setmxcheck', $args); } }
Patched for version 1.0.5.
There are a couple of fixes.
http://sdk.cpanel.net/lib/xmlapi/php..._v1.0.5.tar.gz
Changelog:
* 1.0.5:
* fix bug where api1_query and api2_query would not return JSON data
*
* 1.0.4:
* set_port will now convert non-int values to ints
*
* 1.0.3:
* Fixed issue with set_auth_type using incorrect logic for determining acceptable auth types
* Suppress non-UTF8 encoding when using curl
*
* 1.0.2:
* Increased curl buffer size to 128kb from 16kb
* Fix double encoding issue in terminateresellers()
*
* 1.0.1:
* Fixed use of wrong variable name in curl error checking
* adjust park() to use api2 rather than API1
*
* 1.0
* Added in 11.25 functions
* Changed the constructor to allow for either the "DEFINE" config setting method or using parameters
* Removed used of the gui setting
* Added fopen support
* Added auto detection for fopen or curl (uses curl by default)
* Added ability to return in multiple formats: associative array, simplexml, xml, json
* Added PHP Documentor documentation for all necessary functions
* Changed submission from GET to POST
Matt Dees
Hi,
How to use this api ? I tried to run the api2_example.php file and just got this:
I just want to create and delete a forwarding email address in a specific cpanel account. How to do it using this xml api ?Code:{"cpanelresult":{"data":[],"event":{"result":1},"module":"Email","apiversion":2,"func":"listpopswithdisk"}}
Thanks.
Hi TapanB
You can find the documentation of the XML and JSON apis here:
XML and JSON APIs
Specifically, api2_example.php will list all pop addresses with their disk usage. For this call to work you must provide cPanel user account to check against ($account='somecpanelusername'). If the cpanel user has no email accounts setup for their domain, the result will be an empty 'data' block, but with a 1 in the 'event'=>'result' block. This indicates that the system recognized your query, but doesn't have anything to show you for the variables you've provided. The same result will return if you provide a user that doesn't exist on that system.
will return this, if I have one email account for tester@dave.testCode:$account='dave'; //my username is dave and my domain is dave.test;
if you which to have XML output, just setCode:{"cpanelresult":{"data":[{"diskquota":250,"humandiskquota":"250 MB","user":"tester","_diskused":0,"humandiskused":"None","_diskquota":262144000,"txtdiskquota":250,"diskusedpercent":0,"diskusedpercent20":0,"login":"tester@dave.com","domain":"dave.com","diskused":0,"email":"tester@dave.com"}],"event":{"result":1},"module":"Email","apiversion":2,"func":"listpopswithdisk"}}
Code:$xmlapi->set_output('xml'); //instead of json
David Neimeyer
Integration Developer
sdk.cpanel.net
APIs: XML-API API1 & API2
Check Out: Developer Downloads Integration Blog
Need Support? Support Ticket Developer Forum Feature Requests
Forwarding email addresses is not a WHM function. You should use a cPanel class instead.
If you're wanting to use the xml-api:
To create an email forward you must make this api2 call:
ApiEmail < ApiDocs < TWiki
To delete an email forward you must make an api1 call to Email::delforward, which, to my surprise, is currently undocumented (but will be the future)
The key here is that the first argument (and only argument) to pass to delforward is a string that represents the relationship of the target address (inbound address) and the destination address (where the email gets forwarded to).Code:https://yourserver:2083/xml-api/cpanel? cpanel_xmlapi_apiversion=1 &cpanel_xmlapi_module=Email &cpanel_xmlapi_func=delforward arg-0=someone%40dave.com%3Dnewdestination%40dave.com
if you're using the XMLAPI.php client class, an example usage would be:
Regards,Code:include("xmlapi.php"); $ip = "192.168.1.1"; $account_pass = "secret_passwd"; $account = "dave"; $foward_to_kill = 'someone@dave.com=newdestination@dave.com'; $xmlapi = new xmlapi($ip); $xmlapi->password_auth($account, $account_pass); $xmlapi->set_output("xml"); $xmlapi->set_port('2083'); $result = $xmlapi->api1_query($account, "Email", "delforward", array($forward_to_kill) );
-Dave
David Neimeyer
Integration Developer
sdk.cpanel.net
APIs: XML-API API1 & API2
Check Out: Developer Downloads Integration Blog
Need Support? Support Ticket Developer Forum Feature Requests
Hi,
Ok go deleted working also.
Thanks.
Last edited by TapanB; 03-19-2010 at 06:21 AM.
Hello,
Got add working.
Thanks.
Last edited by TapanB; 03-19-2010 at 06:21 AM.
Hi everyone. i'm having trouble with creating accounts via xml api. Account creation is fine but i want to create account using packages i already created.
fetching packages like above and process this way ;PHP Code:$xml = simplexml_load_string($xmlData);
foreach($xml as $paketler)
{
$paketAdi = $paketler->name;
echo "<option value=\"$paketAdi\">$paketAdi</option>";
}
PHP Code:$paket = $_POST["paket"];
$acct = array( username => $username, password => $password, domain => $domain, pkgname => $paket);
account creation is successful but package is setting "default". How can i create account with packages ( created in whm )
thanks so much. and sorry about my English :/