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.

API2 'delpop' problem

Discussion in 'cPanel Developers' started by bmett, Aug 5, 2010.

  1. bmett

    bmett Well-Known Member

    Joined:
    Aug 5, 2010
    Messages:
    63
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Brisbane, AU
    Hi there,

    Firstly a warm 'Hello' to everyone, this is my first post in this forum and I hope somebody can tell me what I do wrong.

    I successfully integrated the cPanel API in our CMS and was able to use the function 'listpops' to list all email-accounts of a certain useraccount. No problems so far. Next step is to delete a certain email-account.

    I use the following code to test:

    Code:
        $whmusername = "root";
        $whmhash = "myHashKey";     # some hash value
    
        $query = "https://myIP:2087/json-api/cpanel?user=name&cpanel_jsonapi_module=Email&cpanel_jsonapi_func=delpop&cpanel_jsonapi _apiversion=2&domain=myURL.com.au&email=test";
        
        $curl = curl_init();																		# Create Curl Object
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);												# Allow certs that do not match the domain
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);												# Allow self-signed certs
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);								                # Return contents of transfer on curl_exec
        $header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$whmhash);	# Remove newlines from the hash
        curl_setopt($curl,CURLOPT_HTTPHEADER,$header);												# Set curl header
        curl_setopt($curl, CURLOPT_URL, $query);													# Set your URL
        $result = curl_exec($curl);																	# Execute Query, assign to $result
        if ($result == false) {
    	    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
        }
        curl_close($curl);
        
     var_dump($result);
    I use var_dump just to see the result and for testing.
    Unfortunately the respone I get is the following:
    And now the interesting part: If I use the string in $query and put it directly into the browser (while I'm logged in in cPanel) it works!
    Can somebody explain this to me? And before somebody asks. Yes the account and the email do exist. :D I just didnt put the correct value in the code example above.

    Cheers
    Bjorn
     
  2. bmett

    bmett Well-Known Member

    Joined:
    Aug 5, 2010
    Messages:
    63
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Brisbane, AU
    Maybe port problem?

    After a bit more research I found out that I may have used the wrong port in the script above.
    I tried it now with port cpanel-port 2083. This time the authentication failed. Looks like that my authentication code only works with WHM ports. Could that be the case? If so how do I do a proper authentication when using the cPanel-Port 2083?

    But this wouldn't explain why other function work in the script above. E.g. the function 'listpops' works absolutely fine.

    I'm confused :confused:

    Hopefully someone can help me out.

    Cheers,
    Bjorn
     
  3. 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 Bjorn,

    I suggest you take a look at our xml-api PHP client class. I will take care of the authentication stuff for you. You should be able to extend this class to accomodate your CMS. Or, alternatively, you can just take a peak at how we make our cURL connections (the 'curl_query' method in the class).

    With this class you could express you code example as follows:
    Code:
    include_once ('xmlapi.php');
    
    $ip='10.1.1.1'; //your IP
    $hash = file_get_contents('/root/.accesshash'); //or from wherever you have it
    
    $xmlapi = new xmlapi($ip);
    $xmlapi->set_user('root');
    $xmlapi->set_hash($hash);
    $xmlapi->set_http_client('curl'); // or fopen; we recommend curl
    $xmlapi->set_port('2087'); //you can use 2083 if it's API1 or API2 query
    $xmlapi->set_output('json');
    //$xmlapi->set_debug(1);
    
    $user='name';
    $module = 'Email';
    $function = 'delpop'
    $args = array( 'domain' => 'myURL.com.au', 'email' => 'test' );
    
    $result = $xmlapi->api2_query($user, $module, $function, $args);
    
    var_dump($result);
    
    In this example, 'api2_query' prepares your query string and headers then executes the query for you: very clean and simple.

    Here's the sticky thread about the class (pretty long, but some good stuff here and there):
    http://forums.cpanel.net/f42/xml-api-php-class-version-1-0-a-136449.html

    Here's the download link to a tarball that contains the latest version of the class and some examples:
    http://sdk.cpanel.net/lib/xmlapi/php/cp_xmlapi_php_v1.0.5.tar.gz

    You might want to take a look at the Integration blog; there's at least two articles where it's used:
    cPanel - cPanel Integration


    That being said, your direct questions:
    1) Which port?

    a) 2087 can be used all the time. Whether you're making native xml-api calls or cpanel API1/API2 calls. Just make sure you pass 'user=$cpuser' where $cpuser is the cPanel user to act on behalf of. Sidenote, 'user' parameter can expressed as 'cpanel_xmlapi_user'/'cpanel_json_user' and is the preferred convention for future development.

    b) If you use port 2083, you should only be making API1/API2 calls and be authenticating as the actual cPanel user. The 'user'/'cpanel_xmlapi_user'/'cpanel_jsonapi_user' is superfluous but benign. FYI, in the xml-api class we send it anyway *shrug*.

    2) Why didn't 'delpop' work on 2087 and 'listpops' did? I'm not sure; I'm curious if other API2 calls work correctly. You may want to use the API Tracer to figure out what variables exactly are being passed. You might have a look at this blog article about troubleshooting xml-api requests.


    Hopefully, I've clarified some of your issues and provided enough resources for you. Again, I encourage you to leverage the xml-api PHP client class for your own needs. If you're still stuck, let me know :)

    Regards,
    -DavidN
     
  4. bmett

    bmett Well-Known Member

    Joined:
    Aug 5, 2010
    Messages:
    63
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Brisbane, AU
    Hi DavidN,

    Thanks for your very helpful reply.

    I actually found the xml-api PHP client class over the weekend and tried it out even before reading your reply (just back in office today, we had a public holiday yesterday :D) and it worked on first try. The class is very helpful and easy to use and I had no problems integrating it in my code. Of course I always like to write and use my own code but this time you guys just made it to easy for me :)

    Great work with the cPanel API guys. One of the easiest APIs I used so far (and I used quite a bit). Well done!

    Cheers,
    Bjorn
     
Loading...

Share This Page