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.

Problem with CURL in XML api

Discussion in 'cPanel Developers' started by amirem, Oct 17, 2010.

Thread Status:
Not open for further replies.
  1. amirem

    amirem Registered

    Joined:
    Oct 17, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    hi all
    i used this code for access cpanel but get nothing !
    please help me :)
    PHP:
    <?
    $cpanelusername "xxx";
    $cpanelpassword "000";

    $query 
    "http://domain.com:2082/xml-api/cpanel?user=xxx&xmlin=<cpanelaction>
    <module>Ftp</module>
    <func>addftp</func>
    <apiversion>1</apiversion>
    <args>test</args>
    <args>123456</args>
    <args>/</args>
    <args>100</args>
    </cpanelaction>"
    ;


    $curl curl_init();        
    curl_setopt($curlCURLOPT_SSL_VERIFYPEER,0);    
    curl_setopt($curlCURLOPT_SSL_VERIFYHOST,0);     
    curl_setopt($curlCURLOPT_HEADER,0);            
    curl_setopt($curlCURLOPT_RETURNTRANSFER,1);    
    $header[0] = "Authorization: Basic " base64_encode($cpanelusername.":".$cpanelpassword) . "\n\r";
    curl_setopt($curlCURLOPT_HTTPHEADER$header);  
    curl_setopt($curlCURLOPT_URL,$query);            
    $result curl_exec($curl);

    if (
    $result == false) {
        
    error_log("curl_exec threw error \"" curl_error($curl) . "\" for $query");    
    }
    curl_close($curl);

    print 
    $result;
    ?>
     
  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 amirem,

    You have a could of things that need correction. First, I would like to suggest you look at our PHP client class http://sdk.cpanel.net/lib/xmlapi/php/cp_xmlapi_php_v1.0.6.tar.gz

    It has all the cURL code that you'll need. So, you could copy that code. But better, you can just include the class file and spawn an object instance. Here's an example of how to add an ftp user using out PHP class

    PHP:
    <?php
    include("xmlapi.php");

    $ip "domain.com";
    $user_name "myuser";
    $user_pass "secret1";

    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth($user_name$user_pass);
    $xmlapi->set_port('2083');
    //$xmlapi->set_debug(1);

    $module 'Ftp';
    $function 'addftp';
    $args = array(
      
    'user' => 'testftpuser',
      
    'pass' => 'testpass',
      
    'quota' => 0
      
    'homdir' => '/',
    );

    $result_obj $xmlapi->api2_query($user_name$module$function$args);
    ?>
    uncommiting the debug method, will give you more info when you execute it. For example, it will provide you the URL that it's using:
    Code:
    URL: https://domain.com:2083/xml-api/cpanel
    DATA: user=testftpuser&pass=testpass&quota=0&homdir=%2F&cpanel_xmlapi_user=myuser&cpanel_xmlapi_module=Ftp&cpanel_xmlapi_func=addftp&cpanel_xmlapi_apiversion=2
    
    So, other things that you've got going on, besides the cURL issue:

    1) You should use API2 call over API1 calls if possible. There's only a few modules that are available for both; 'Ftp' is one of them

    2) If you are making XML-API calls, it's best to use the "fast mode" construction for the URL arguments. Examples are here: CallingAPIFunctions < AllDocumentation/AutomationIntegration < TWiki . We currently support both, but it's a lot faster and easier to use "fast mode".

    Best Regards,
    -DavidN
     
  3. jerome.chevreau

    Joined:
    Aug 26, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Hi there,

    I am using the xml-api. Recently i am experiencing problems creating accounts.
    Using fsockopen -> it creates the account but the code breaks therefore i dont receive any response.
    Using CURL -> it looks more promissing - account get created but the response returned is "sorry group already exist ..."

    Confusion comes when i try the same code on my localhost (xampp) and it runs perfectly and response is also returned.

    Does anybody might have encountered that or have a sol please.
    I am on the fourth day of sorting it out and still dont understand how and why it happens.

    Thanks a lot for any help,

    Regards
     
    #3 jerome.chevreau, Apr 7, 2011
    Last edited: Apr 7, 2011
  4. 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 Jerome,

    What port are you using? do you have OpenSSL support for the PHP binary (not in your xampp environment, but the one the first one you mention). Most of the time, fopen stuff fails due to a request over https and OpenSSL in not compiled in.

    Are you using the XML-API client class? I ask because it usesfopen() and not fsockopen(). IIRC, you should be able to use fsockopen(). But typically, unless I need a particular socket type, I just use context streams with fopen()...just an opinion.

    If you get back an error, like "sorry already exists..." there should be no account created in cPanel/WHM. That type of error signals a failure in the creation process. Please confirm that the account wasn't previously there.

    Regards,
    -DavidN
     
Loading...
Thread Status:
Not open for further replies.

Share This Page