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.

API Issues

Discussion in 'cPanel Developers' started by Monotoko, Mar 8, 2011.

  1. Monotoko

    Monotoko Member

    Joined:
    Mar 1, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Hi Guys,

    Getting quite fustrated, I have a page that is supposed to be accessing the API, the problem is I just keep hitting the 404 error page whenever I run it. It is the default example code from the authentication tutorial so it should be working!

    For some reason the $result variable is just holding a "404 not found" page from my blog... (my blog is in the root folder, this script is in a directory named "cp")

    Help? :confused:

    Code:
    <?
    
    $whmusername = "root";
    $whmpassword = "<snip>";
    
    $query = "<snip>";
    
    $curl = curl_init();		
    # Create Curl Object
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);	
    # Allow self-signed certs
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); 	
    # Allow certs that do not match the hostname
    curl_setopt($curl, CURLOPT_HEADER,0);			
    # Do not include header in output
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);	
    # Return contents of transfer on curl_exec
    $header[0] = "Authorization: Basic " . base64_encode($whmusername.":".$whmpassword) . "\n\r";
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);  
    # set the username and password
    curl_setopt($curl, CURLOPT_URL, $query);			
    # execute the query
    $result = curl_exec($curl);
    if ($result == false) {
    	error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");	
    # log error if curl exec fails
    }
    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 Monotoko,

    I tried the following, which I cut and pasted from the authentication documentation, and with the proper values in those first three variables, it works just fine for me

    PHP:
    <?php 
    $whmusername 
    "root";
    $whmpassword "s3cr3t!";

    $query "https://10.1.1.1:2087/xml-api/version";

    $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($whmusername.":".$whmpassword) . "\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");    
    # log error if curl exec fails
    }
    curl_close($curl);

    print 
    $result;
    Could you post the URL that you're attempting to make? The 404 error code is for malformed or non-existent URLs.

    Regards,
    -DavidN

    PS. a side note, that documentation page has "\n\r" for the newline in the header auth string. I think it should be "\r\n" ie CR+LF. I ran the example with both, and for this simple script, it worked fine regardless. However, for integrity's sake, I'll ask Documentation to update it.
     
Loading...

Share This Page