danny0n

Member
Jan 21, 2010
8
0
51
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($query, false, $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.
 

danny0n

Member
Jan 21, 2010
8
0
51
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.
 

MattDees

Well-Known Member
Apr 29, 2005
416
1
243
Houston, TX
cPanel Access Level
Root Administrator
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.
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.
 

danny0n

Member
Jan 21, 2010
8
0
51
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