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.

Add Access Host on RemoteMySQL

Discussion in 'cPanel Developers' started by anka86, Dec 5, 2011.

  1. anka86

    anka86 Registered

    Joined:
    Nov 24, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Timisoara, Romania, Romania
    cPanel Access Level:
    Website Owner
    Hi,

    I want to develop an application in Visual Fox Pro 9 with online database. For that, I have to register every client's IP into CPanel, on RemoteMySQL. I found this code in CPanel documentation, created a test.php file with the code bellow, 755 right permission on file, but when I access mydomain.php/test.php I recieve 500 Internal Error, request timeout. I can't increase the timeout connection, because I don't have an VPS server.

    I need some help please.

    Kind regards,
    Anca

    PHP:
    <?
    $username "myCpanelusername";$password "myCpanelpassword";
    $query "https://server.com:2087/xml-api/cpanel?cpanel_xmlapi_user=myCpanelusername&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=addhost&cpanel_xmlapi_apiversion=1&arg-0=mydomain.com";
    $curl curl_init();# Create Curl Objectcurl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);# Allow self-signed certscurl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);# Allow certs that do not match the hostnamecurl_setopt($curl, CURLOPT_HEADER,0);# Do not include header in outputcurl_setopt($curl, CURLOPT_RETURNTRANSFER,1);# Return contents of transfer on curl_exec$header[0] = "Authorization: Basic " . base64_encode($username.":".$password) . "\n\r";curl_setopt($curl, CURLOPT_HTTPHEADER, $header);# set the username and passwordcurl_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. storminternet

    storminternet Well-Known Member

    Joined:
    Nov 2, 2011
    Messages:
    462
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Hi,

    Have you checked the ownership of test.php ? If suphp is enabled on server then it would throw the internal server error if file has incorrect ownership.
     
  3. anka86

    anka86 Registered

    Joined:
    Nov 24, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Timisoara, Romania, Romania
    cPanel Access Level:
    Website Owner
    An CPanel technical support operator suggested me to change the port from 2087 to 2083. I am a newbie in PHP and I have no knowledge about suPHP. I'll ask my hosting provider if it is enabled. Any advice?

    Thanks,
    Anca
     
  4. anka86

    anka86 Registered

    Joined:
    Nov 24, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Timisoara, Romania, Romania
    cPanel Access Level:
    Website Owner
    Response from my hosting provider: suPhp is enabled.
    file permissions: 644 or 755?
     
    #4 anka86, Dec 6, 2011
    Last edited: Dec 6, 2011
  5. storminternet

    storminternet Well-Known Member

    Joined:
    Nov 2, 2011
    Messages:
    462
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Hi,

    File permissions on suphp enabled server would be 644. 755 is applicable for folders.
     
  6. anka86

    anka86 Registered

    Joined:
    Nov 24, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Timisoara, Romania, Romania
    cPanel Access Level:
    Website Owner
    Now it is set to 644. I looked into error_log file on server and I found this error:
    curl_exec threw error "couldn't connect to host" for
    https://server.com:2083/xml-api/cpanel?cpanel_xmlapi_user=myCpanelusername&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=addhost&cpanel_xmlapi_apiversion=1&arg-0=mydomain.com
     
  7. storminternet

    storminternet Well-Known Member

    Joined:
    Nov 2, 2011
    Messages:
    462
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Better to debug your code or get back to your hosting provider for more troubleshoot on this.
     
  8. 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 anka86,

    You should consider using the XML-API PHP client class found on our GitHub page. It will construct your HTTP request and send/receive it via cURL or fopen for you; that class will do all the heavy lifting. You don't have to be concerned about the HTTP headers or cURL functions. Your script would look something like this if you're a reseller or root and want to add the access host on behalf of accounts you have authority over (aka, your clients).
    PHP:
    include "xmlapi.php";

    $ip 'server.com';
    $auth_user 'your_account';
    $auth_pass 'your_pass';

    $obj = new xmlapi($ip);
    $obj->password_auth($auth_user$auth_pass);

    //$obj->set_debug(1);

    $module 'Mysql';
    $function 'addhost';
    $args = array('your_remote_mysql_db_address_or_fqdn');

    $list_of_cpanel_accounts = array('user1''user2''etc');

    foreach (
    $list_of_cpanel_accounts as $cpuser) {
        
    $cpuser 'some_cpanel_account_you_have_authority_over';
        
    $result $obj->api1_query($cpuser$module$function$args);
        
    // do something with result //
    }
    That example will utilize the WHM port (2087), once the authentication is validated, your API request (Mysql::addhost) is performed as the $cpuser.

    If you don't have a reseller or root account, but have several individual cPanel accounts, you'd change up the script like this:
    PHP:
    include "xmlapi.php";

    $ip 'server.com';

    $obj = new xmlapi($ip);
    $obj->set_port('2083');

    //$obj->set_debug(1);

    $module 'Mysql';
    $function 'addhost';
    $args = array('your_remote_mysql_db_address_or_fqdn');

    $list_of_cpanel_accounts = array('user1'=>'pass1''user2'=>'pass2''etc'=>'etc');

    foreach (
    $list_of_cpanel_accounts as $cpuser=>$pass) {
        
    $obj->password_auth($cpuserpass);
        
    $result $obj->api1_query($cpuser$module$function$args);
        
    // do something with result //
    }
    This variation will authenticate at the cPanel account level as the user. This is done in two ways: A) you must use port 2083 and B) you must authenticate as the end-user cPanel account. (these statements are true for all XML-API requests, not just when you use the xmlapi.php class).

    Also note I've commented out the debug method in both scripts. You can uncomment that and, depending on your PHP ini settings and how you're executing the script, you will see the HTTP request URL, Headers, and the raw response and a dump of the SimpleXML obj that is created from the response.

    You don't have to use this class, but it will make you life easier if you have other scripts to right in the future.

    Regards,
    -DavidN
     
Loading...

Share This Page