首页 > 解决方案 > 在订购和终止时收集产品的自定义字段数据

问题描述

我目前正在 WHMCS 中处理一些自动化的事情。我在 WHMCS 中提供了几种定制产品:

Linux 产品 1 Linux 产品 2 Windows 产品 1 Windows 产品 2

我想在订单接受和服务从 WHMCS 终止时执行 bash 脚本。该脚本需要一个参数(IP 地址),它是上述产品的自定义字段。脚本应该获取这个包含 IP 地址的自定义字段数据,并比较产品是 Linux 还是 Windows,然后将其传递给脚本,如下所示:

我猜可以使用 WHMCS AcceptOrder 和 AfterModuleTerminate 挂钩。但不确定我们如何获取这些自定义字段数据并在那里比较钩子 PHP 代码中的产品。任何人都可以对此有所了解或帮助我们正确编码。

任何回应将不胜感激!

已经创建了 Bash 脚本,并且运行良好。我是 WHMCS 钩子和 PHP 的新手,所以被困在这里。

标签: bashhookwhmcs

解决方案


使用产品模块:产品模块并实现方法_CreateAccount($params) ,您可以在$params变量中找到“CustomFields” 。这是执行python脚本的示例:

<?php 
function my_proc_execute($cmd)
{
    $output='';
    try
    {
        $descriptorspec = array(
                0 => array("pipe", "r"), //STDIN
                1 => array("pipe", "w"), //STDOUT
                2 => array("pipe", "w"), //STDERR
                );
        $cwd = getcwd(); $env = null; $proc = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env);
        $buffer=array();
        if(is_resource($proc))
        {
            fclose($pipes[0]);
            stream_set_blocking($pipes[1], 1);
            stream_set_blocking($pipes[2], 1);
            stream_set_timeout($pipes[1],500);
            stream_set_timeout($pipes[2],500);
            while (!feof($pipes[1]))
            {
                $line = fread($pipes[1], 1024);
                if(!strlen($line))continue;
                $buffer[]=$line;
            }
            while(!feof($pipes[2]))
            {
                $line=fread($pipes[2], 1024);
                if(!strlen($line))continue;
                $buffer[]=$line;
            }
            $output=implode($buffer);
            fclose($pipes[1]);fclose($pipes[2]);
            $return_value = proc_close($proc);
        }
        else $output = "no resource; cannot open proc...";
    }catch(Exception $e){$output = $e->getMessage();}
    return $output;
}
function mymodule_CreateAccount($params)
{
    $myData=$params['customfields']['the_name_of_field'];
    $to_send_arr=array();
    $to_send_arr['is_new']='0';
    $to_send_arr['id']='xyz';
    $to_send_arr['url']='my';
    $to_send_arr['display_name']=$myData;
    $exewin="c:/Python27/python.exe C:/xampp_my/htdocs/my/modules/servers/xxx/pyscripts/create_data.py";
    $exelinux="/var/www/html/modules/servers/xxx/pyscripts/create_data.py";
    $command=$exewin . ' ' . escapeshellcmd(base64_encode(json_encode($to_send_arr)));
    $output=my_proc_execute($command);
    $arr=json_decode($output,true);
}
?>

Python

import sys
import json
import time
import base64

if len(sys.argv) != 2:
    print 'Error in the passed parameters for Python script.', '<br>'
    sys.exit()
json_data = json.loads(base64.b64decode(sys.argv[1]))
id= json_data['id']

推荐阅读