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.

AddonDomain Help with Curl

Discussion in 'cPanel Developers' started by chrislarge305, Jun 7, 2010.

  1. chrislarge305

    chrislarge305 Registered

    Joined:
    Dec 2, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Hi there,

    This was working a while ago but I think a Cpanel Update has stopped it from working since that is the only thing that has changed since. Could anyone just check through the following code to make sure that there isnt anything obvious I have checked the docs a few times over but cant seem to see anything there.

    PHP:
    function addDomainToCpanel($url,$cpanelusername,$cpanelpassword)
    {
    $pass "******";
    $queryold "$url:2083/xml-api/cpanel?user=$cpanelusername&xmlin=<cpanelaction><module>AddonDomain</module><func>addaddondomain</func><args><dir>public_html/domains/domains/$url</dir><newdomain>$url</newdomain><subdomain>$url</subdomain><pass>$pass</pass></args></cpanelaction>
    "
    ;
    $query "$url:2083/json-api/cpanel?user=$cpanelusername &cpanel_xmlapi_module=AddonDomain&cpanel_xmlapi_func=addaddondomain &cpanel_xmlapi_version=2&dir=public_html/domain/domains/$url&newdomain=$url&subdomain=$url&pass=$pass";

    $curl curl_init();        
    # Create Curl Object
    curl_setopt($curlCURLOPT_SSL_VERIFYPEER,0);    
    # Allow self-signed certs
    curl_setopt($curlCURLOPT_SSL_VERIFYHOST,0);     
    # Allow certs that do not match the hostname
    curl_setopt($curlCURLOPT_HEADER,0);            
    # Do not include header in output
    curl_setopt($curlCURLOPT_RETURNTRANSFER,1);    
    # Return contents of transfer on curl_exec
    $header[0] = "Authorization: Basic " base64_encode($cpanelusername.":".$cpanelpassword) . "\n\r";
    curl_setopt($curlCURLOPT_HTTPHEADER$header);  
    # set the username and password
    curl_setopt($curlCURLOPT_URL$query);            
    # execute the query
    $result curl_exec($curl);
    if (
    $result == false) {
        
    error_log("curl_exec threw error \"" curl_error($curl) . "\" for $query");
        echo 
    curl_error($curl);
        echo 
    $query;
        return 
    0;    
    # log error if curl exec fails
    }
    else
    {
        echo 
    curl_error($curl);
        echo
    "<br><br>";
        echo 
    $query;
        echo
    "<br><br>";
        return 
    1;
    }

    curl_close($curl);
    If I echo the result of the curl it seems to just take me to the cpanel page.

    Thanks

    Regards

    Chris
     
  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 Chris,

    I think your problem is that in variable $query, you're cPanel URL is pointed to the JSON API (json-api) yet your parameter keys are for the XML API.
    Code:
    //direct transcription of your $query string for a proper json-api query
    $query = "$url:2083/json-api/cpanel?"
                   ."user=$cpanelusername"
                   ."&cpanel_jsonapi_module=AddonDomain"
                   ."&cpanel_jsonapi_func=addaddondomain"
                   ."&cpanel_jsonapi_version=2"
                   ."&dir=public_html/domain/domains/$url"
                   ."&newdomain=$url"
                   ."&subdomain=$url"
                   ."&pass=$pass";
    
    If you just wanted XML results, then just leave the parameters as 'cpanel_xmlapi_X" and change the first part of the URL back to :2083/xml-api/cpanel?

    It that doesn't work, post your $result and well get it figured out.
    -David

    PS. I find it unusual that the base URL of $query, $url, is also the same as what you provide for the new addon domain's 'newdomain' string. I guess I'm missing something.

    PSS. You may want to consider adding some logic similar to the code block below so that you can POST
    Code:
    // Assumes CURLOPT_URL is something like https://$domain:2083/xml-api/cpanel
    //$postdata is a regular query string made with http_build_query()
    //$authstr is your base64_encode with \r\n
    
    $header[0] = $authstr .
    	"Content-Type: application/x-www-form-urlencoded\r\n" .
    	"Content-Length: " . strlen($postdata) . "\r\n" . "\r\n" . $postdata;
    		
    	curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    	curl_setopt($curl, CURLOPT_POST, 1);
    
    Have you checked out our XMLAPI.php client class? I can make this sort of thing a breeze. http://sdk.cpanel.net/lib/xmlapi/php/cp_xmlapi_php_v1.0.5.tar.gz
     
Loading...

Share This Page