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.

Null Response To cPanel API Calls From Silex Closure [resolved]

Discussion in 'cPanel Developers' started by MattDr2, Jun 18, 2015.

  1. MattDr2

    MattDr2 Well-Known Member
    PartnerNOC

    Joined:
    Feb 19, 2003
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Norman, OK
    I'm developing a cPanel plugin using Silex. Silex runs a lot of code inside closures, and I'm having difficulty using the cPanel API (/usr/local/cpanel/php/cpanel.php) from within a closure.

    In the global context, cPanel API queries return properly, no issues there. This is working fine:
    Code:
    include("/usr/local/cpanel/php/cpanel.php");
    $cpanel = new CPANEL();
    $domains = $cpanel->uapi('DomainInfo', 'list_domains');
    //...
    $cpanel->end();
    
    But, if I remove the above code and instead try to activate the CPANEL class via a closure, like below, I receive null responses to all requests. I don't receive any exceptions, only null responses to requests. In reviewing the cpanel.php code, I would expect exceptions rather than null responses.

    Code:
    $app['cpanel'] = $app->share(function () {
        include("/usr/local/cpanel/php/cpanel.php");
        return new CPANEL();
    });
    // Returns NULL
    $domains = $app['cpanel']->uapi('DomainInfo', 'list_domains');
    
    Further, if I try to activate the class and use it within its own closure, the issue is the same. I also can't use a globally activated CPANEL class from within a closure (below).

    Code:
    include("/usr/local/cpanel/php/cpanel.php");
    $cpanel = new CPANEL();
    
    $app->get('/test/{domain}', function ($domain, Application $app) use ($cpanel) {
        // Returns NULL
        $mxList= $cpanel->uapi('Email', 'list_mxs');
    });
    
    // Returns valid data
    $mxList= $cpanel->uapi('Email', 'list_mxs');
    
    //...
    $cpanel->end();
    
    Is this expected behavior that the cPanel PHP socket cannot be accessed inside a closure? I've scoured the net but can't find any indication that sockets in general shouldn't be accessible in this way, and I can't see any reason this would be restricted when looking through the code in the cpanel.php file. If anything, I should get some kind of exception returned.

    Edit: I should also add that this occurs inside any PHP closure regardless of the use of Silex functions as a wrapper.

    Any help would be much appreciated.
     
    #1 MattDr2, Jun 18, 2015
    Last edited: Jun 18, 2015
  2. MattDr2

    MattDr2 Well-Known Member
    PartnerNOC

    Joined:
    Feb 19, 2003
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Norman, OK
    It looks like the issue is that the CPANEL class is being destructed (__destruct()) before closures are called, which is causing this problem.
     
  3. MattDr2

    MattDr2 Well-Known Member
    PartnerNOC

    Joined:
    Feb 19, 2003
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Norman, OK
    Got it - it's my fault. :) I was running $cpanel->end() before running the Silex application.
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    648
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator

Share This Page