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 file_get_contents() error

Discussion in 'cPanel Developers' started by danny0n, Jul 12, 2010.

  1. danny0n

    danny0n Member

    Joined:
    Jan 21, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Hello, I'm trying to use json Fast Mode api from within a whm module and I keep getting this error:

    Warning: file_get_contents(http://host_here:2086/json-api/cpanel?cpanel_jsonapi_module=Cron&cpanel_jsonapi_func=listcron) [function.file-get-contents]: failed to open stream: Redirection limit reached, aborting in /usr/local/cpanel/whostmgr/docroot/module/gui/whm/main.php on line 48

    A POC of my code would be:

    PHP:
    cpanelSendQuery("cpanel?cpanel_jsonapi_module=Cron&cpanel_jsonapi_func=listcron");
       
        function 
    cpanelSendQuery($query) {
            
    $ip 'ip here';
            
    $hash 'hash key here';

            
    $query "http://".$ip.":2086/json-api/".$query;

            
    $context stream_context_create(
                            array(
                                    
    'http' => array(
                                            
    'allow_self_signed' => true,
                                            
    'method' => 'POST',
                                            
    'header' => "Authorization: WHM ".$_ENV['REMOTE_USER'].":".preg_replace("'(\r|\n)'","",$hash)."\r\n"
                                    
    )
                            )
                    );

            
    $result file_get_contents($queryfalse$context);

            return 
    $result;

        }
    if I use api1 and api2 calls like /json-api/listips it works.

    Another problem is that I cannot use https. If I change the protocol to https and port to 2087 I always get a "No such file or directory" on any query, fast mode or not.

    This would be a lot easier with curl I guess but the WHM's php doesn't have curl installed and I can't figure it out how to install it.

    Thank you for any assistance you can provide.
     
  2. cPanelCraig

    cPanelCraig Member
    Staff Member

    Joined:
    Apr 29, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
  3. danny0n

    danny0n Member

    Joined:
    Jan 21, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    I've tried the script on 2 production servers and on my dev server. The script didn't work on either server.

    I've tried XML API PHP but I have pretty much the same issues:

    With:

    PHP:
    include("xmlapi.php");
    $account 'root';
    $ip "__IP HERE__";
    $root_pass "__PASS HERE__";

    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth("root",$root_pass);
    $xmlapi->set_output('array');
    $xmlapi->set_debug(1);

    print_r($xmlapi->api2_query($account"Cron""listcron" ));
    I get:

    Warning: file_get_contents(https://.....:2087/xml-api/cpanel) [function.file-get-contents]: failed to open stream: No such file or directory in /usr/local/cpanel/whostmgr/docroot/module/xmlapi.php on line 724


    And with:
    PHP:
    define('XMLAPI_USER_SSL''1');
    define('XMLAPI_USE_SSL''0');

    include(
    "xmlapi.php");
    $account 'root';
    $ip "__IP HERE___";
    $root_pass "__PASS HERE__";

    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth("root",$root_pass);
    $xmlapi->set_port(2086);
    $xmlapi->set_output('array');
    $xmlapi->set_debug(1);

    print_r($xmlapi->api2_query($account"Cron""listcron" ));
    I get:
    Warning: file_get_contents(http://....:2086/xml-api/cpanel) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /usr/local/cpanel/whostmgr/docroot/module/xmlapi.php on line 724

    but if instead of print_r($xmlapi->api2_query($account, "Cron", "listcron" )); i use something like print_r($xmlapi->listips()); it works. This is exactly what works in my version of the script. Non fast mode, non https query.
     
  4. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    That's very special.

    Have tried the steps in: Troubleshooting the XML-API - cPanel Integration ?

    If you have and still can't see the source of the problem please open a ticket a tickets.cpanel.net/submit/ with the subject "ATTN: Matt D" and include access to source, etc so that I can look into this issue more thoroughly.
     
  5. danny0n

    danny0n Member

    Joined:
    Jan 21, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    It seems I get a lot of this in error log.

    Code:
    [2010-07-13 21:51:06 +0400] info [cpanel] api/cpanel call failed. Module name is required.
    [2010-07-13 21:51:06 +0400] warn [cpanel] User file '/var/cpanel/users/root' is empty or non-existent. at /usr/local/cpanel/Cpanel/Config/LoadCpUserFile.pm line 35
            Cpanel::Config::LoadCpUserFile::load('root') called at /usr/local/cpanel/Cpanel/Config/LoadCpUserFile.pm line 158
            Cpanel::Config::LoadCpUserFile::loadcpuserfile('root') called at /usr/local/cpanel/Cpanel.pm line 96
            Cpanel::initcp(Cpanel=HASH(0x9e62a00)) called at cpanel line 269
     
  6. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    That makes it alot more clear, could you send me the specific URL you are sending via the class?
     
Loading...

Share This Page