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.

Trying to create to connect ot cpanel

Discussion in 'cPanel Developers' started by anthonyg, Apr 22, 2011.

  1. anthonyg

    anthonyg Registered

    Joined:
    Apr 22, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    I am having a very hard time trying to connect to cpanel. I am using a VPN to host the site. and I am also using WHM/CPANEL

    I am trying to connect using PHP

    below is my code
    PHP:
    $cPanel = new cPanel('rmseast.com''rmseast''wordpass1'2082true);
    $domains $cPanel->listDomains();
    Here is the HTTP class I use to connect every time
    PHP:
    class HTTP
    {
        function 
    HTTP($host$username$password$port 2082$ssl ''$theme 'x')
        {
            
    $this->ssl $ssl 'ssl://' '';
            
    $this->username $username;
            
    $this->password $password;
            
    $this->theme $theme;
            
    $this->auth base64_encode($username ':' $password);
            
    $this->port $port;
            
    $this->host $host;
            
    $this->path '/frontend/' $theme '/';
        }

        function 
    getData($url$data '')
        {
            
    $url $this->path $url;
            if(
    is_array($data))
            {
                
    $url $url '?';
                foreach(
    $data as $key=>$value)
                {
                    
    $url .= urlencode($key) . '=' urlencode($value) . '&';
                }
                
    $url substr($url0, -1);
            }
            
    $response '';
            
    $fp fsockopen($this->ssl $this->host$this->port);
            if(!
    $fp)
            {
                return 
    false;
            }
            
    $out 'GET ' $url ' HTTP/1.0' "\r\n";
            
    $out .= 'Authorization: Basic ' $this->auth "\r\n";
            
    $out .= 'Connection: Close' "\r\n\r\n";
            
    fwrite($fp$out);
            while (!
    feof($fp))
            {
                
    $response .= @fgets($fp);
            }
            
    fclose($fp);
            return 
    $response;
        }
    }
    Here is the cpanel class

    PHP:
    class cPanel
    {
        
    /**
      * Creates an object to manipulate cPanel
      * @param string $host cPanel host without leading http://
      * @param string $username cPanel username
      * @param string $password cPanel password
      * @param int $port cPanel port, default to 2082. Change to 2083 if using SSL
      * @param bool $ssl False for http (default), true for SSL (requires OpenSSL)
      * @param string $theme cPanel theme, (forward compatibility- 'x' theme currently required)
      * @return cPanel
      */
        
    function cPanel($host$username$password$port 2082$ssl false$theme 'x')
        {
            
    $this->HTTP = new HTTP($host$username$password$port$ssl$theme);
        }

        function 
    listDomains()
        {
            
    $domainList = array();
            
    preg_match_all('/<option value="([^"]*)/'$this->HTTP->getData('mail/addpop2.html'), $domainList);
            if(
    count($domainList[1]) > 0)
            {
                return 
    $domainList[1];
            }
            return 
    false;
        }
    }
     
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi anthonyg,

    You should take a look at our APIs. We have two PHP clients that you can use to make your remote API calls with (instead of having to craft your own, like in your example).

    sdk.cpanel.net (all API documentation)
    PHP XML-API client (there are many threads in the forum about this client)
    PHP PublicAPI client (BETA)

    If you have any specific question, let us know.

    Regards,
    -DavidN
     
  3. anthonyg

    anthonyg Registered

    Joined:
    Apr 22, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Thanks for the reply. The information you gave work out great. So far I am able to create new accounts in the whm using the demo code. I am having issues creating emails though. Below is my code. I intentionally changed the ip address and password_auth for security reasons. I get the following error

    PHP:
    include("../xmlapi.php");

    $ip '60.32.65.30';

    $account "mydomain.com";
    $email_user "somerandomuser";
    $email_password "adfm90f1m3f0m0adf";
    $email_domain "mydomain.com";
    $email_query '10';
    $email_quota '250';

    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth("roots",'34df354fdgrtrr4');
    $xmlapi->set_output('json');

    $xmlapi->set_debug(1);
    print 
    $xmlapi->api1_query($account"Email""addpop", array($email_user$email_password$email_quota$email_domain) );
     
  4. anthonyg

    anthonyg Registered

    Joined:
    Apr 22, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    nevermind, I put mydomain.com for account name instead of using the username mydomain
     
Loading...

Share This Page