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.

$argv empty/missing parameters

Discussion in 'cPanel Developers' started by milakfan, Dec 16, 2012.

  1. milakfan

    milakfan Member

    Joined:
    Dec 16, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    I'm trying to use $argv as an array (for example $opts['user']) but the parameters don't exist, I've been stuck on this for about 2 hours, if anyone can help I would really appreciate it!

    I am trying to make a hook (in accounts create, stage: post)

    this is in the hook file /usr/local/cpanel/hooks/custom/test_hook.php

    PHP:
    function argv2array ($argv) { $opts = array(); $argv0 array_shift($argv);
      while(
    count($argv)) { $key array_shift($argv); $value array_shift($argv); $opts[$key] = $value; } return $opts;
    }

    $opts argv2array($argv);

    (this is the code suggested at
    cPanel & WHM Script Hooks
    Converting hashes passed via ARGV
    )

    I need to use the parameters of opts, for example:

    $opts['contactemail']
    $opts['user']

    but these are all blank.


    I've checked that
    register_argc_argv = On
    in php.ini

    I tried to print $argv but it seems that only $argv[0] exists which just prints out the file location of the hook
    $argv[1] doesn't print anything

    Am I doing something wrong?



    I'm not sure if this could be a reason but I've noticed that
    /scripts/postwwwacct
    is written in perl

    But I need to make my hook in php to interface with softaculous

    How can I get the variables from perl into the php hook script?

    thanks



    thanks!
     
    #1 milakfan, Dec 16, 2012
    Last edited: Dec 16, 2012
  2. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    You can get more information about the contents of $argv with var_dump($argv); or print_r($argv).

    It's possible that cPanel is not populating it or that some of the contents of the array are null strings.

    You should certainly consider using the new standardized hooks system instead of the script hooks. It provides better error reporting, more events and the ability to use multiple hooks per event:

    Getting Started action code as a script
     
  3. milakfan

    milakfan Member

    Joined:
    Dec 16, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Hi, thanks for your reply.

    (I think) I am using the new standardized hooks system.

    I had added it to the hooks and turned on the bug reporting.


    I tried it in perl but it is empty too :(





    [2012-12-17 07:40:41 +0100] info [whostmgr5] End STDERR from hook
    [2012-12-17 07:40:41 +0100] info [whostmgr5] Script hook returned an invalid response:
    [2012-12-17 07:40:41 +0100] info [whostmgr5] script: /usr/local/cpanel/hooks/custom/autowp.php
    [2012-12-17 07:40:41 +0100] info [whostmgr5] response:
    array(1) {
    [0]=>
    string(41) "/usr/local/cpanel/hooks/custom/autowp.php"
    }
    Array
    (
    [0] => /usr/local/cpanel/hooks/custom/autowp.php
    )
    not installed
    [2012-12-17 07:40:41 +0100] info [whostmgr5] -- End Garbage output --
    [2012-12-17 07:40:41 +0100] info [whostmgr5] ---debug_hooks---
    [2012-12-17 07:40:41 +0100] info [whostmgr5] msg: Finished execution of script hook.
    [2012-12-17 07:40:41 +0100] info [whostmgr5] context: Whostmgr::Accounts::Create
    [2012-12-17 07:40:41 +0100] info [whostmgr5] stage: post
    [2012-12-17 07:40:41 +0100] info [whostmgr5] result: array(1)
    [2012-12-17 07:40:41 +0100] info [whostmgr5] point: main
    [2012-12-17 07:40:41 +0100] info [whostmgr5] id: fAMlx5t5rdS4xqTIvWz9iijB
    [2012-12-17 07:40:41 +0100] info [whostmgr5] hook: /usr/local/cpanel/hooks/custom/autowp.php
    [2012-12-17 07:40:41 +0100] info [whostmgr5] escalateprivs: 0
    [2012-12-17 07:40:41 +0100] info [whostmgr5] weight: 100
     
  4. milakfan

    milakfan Member

    Joined:
    Dec 16, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Do you know if I have to "do something" to set the argv to use it in a post stage hook?

    for example do i need the code in "The following boilerplate can be used for a PHP script: "?

    thanks!
     
  5. milakfan

    milakfan Member

    Joined:
    Dec 16, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    I think I got it working

    I used the "The following boilerplate can be used for a PHP script:" code
    and I realized that I can get the data in the array $input['data']

    I guess argv doesn't work
     
  6. 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
    I can think of three reasons that $argv is not populated as you expect:

    1) the PHP ini setting that determines if $argv is automatically generated is set to off. This can be common if the SAPI is CGI.

    2) $argv is referenced in a scope other than 'main'. IIRC, $argv is NOT global. It is only directly accessible in a script when it is not in a function or other scoping construct. It must be passed to those constructs as an argument

    3) there are no arguments! The data is likely coming via the STDIN stream and thus there are no arguments passed to the script.
    Code:
    echo 'some arguments' | php -f my_hook.php
    // vs.
    php -f my_hook.php -- 'some' 'arguments'
    
    Likely, you've encountered the third item since you can use the example and $input has the expected arguments.

    Regards,
    -DavidN
     
  7. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    The new system does not use $argv, it passes the data via STDIN. Here's the sample function from the page I linked that shows you how to read STDIN:

    Code:
    function get_passed_data() {
        $raw_data;
        $stdin_fh = fopen('php://stdin', 'r');
        if ( is_resource($stdin_fh) ) {
            stream_set_blocking($stdin_fh, 0);
            while ( ($line = fgets( $stdin_fh, 1024 )) !== false ) {
                $raw_data .= trim($line);
            }
            fclose($stdin_fh);
        }
        if ($raw_data) {
            $input_data = json_decode($raw_data, true);
        } else {
            $input_data = array('context'=>array(),'data'=>array(), 'hook'=>array());
        }
        return $input_data;
    }
    
     

Share This Page