首页 > 解决方案 > 如何使用WHMCS Local api(内部API)

问题描述

如何使用 WHMCS LocalAPI (InternaAPI)

嗨,我在使用 WHMCS LocalAPI 时遇到问题 WHMCS 文档非常差,并且在我运行此代码时不清楚这个问题,出现一个空白页面并且发生了任何事情

<?php
require('../init.php');
require('../includes/api.php');
$command = 'AddOrder';
$postData = array(
    'clientid' => '1',
    'domain' => array('domain1.com'),
    'billingcycle' => array('annually'),
    'domaintype' => array('register',),
    'regperiod' => array(1),
    'nameserver1' => 'ns1.demo.com',
    'nameserver2' => 'ns2.demo.com',
    'paymentmethod' => 'zarinpalgw',
);
$adminUsername = 'myadminname'; // Optional for WHMCS 7.2 and later

$results = localAPI($command, $postData, $adminUsername);
print_r($results);


?>

我希望在运行此代码后添加订单外部 API 非常慢并且由于某些原因不适合我,例如我有一个动态 IP 和外部 API 使用静态 IP,因为 IP 必须在 WHMCS->常规设置->安全中识别

标签: apiwhmcs

解决方案


您示例中的内部 API代码看起来应该可以工作。暂时启用 PHP 错误可以帮助缩小此问题的确切原因(设置 > 常规设置 > 其他 > 显示错误),尽管我相信这是由于您在 PHP 文件中初始化 WHMCS 环境的方式。

WHMCS 提供了有关构建自定义页面的特定指南,这似乎是您在提供的示例中尝试执行的操作。自定义 PHP 文件必须位于根 WHMCS 目录中,但require('../init.php');表示您的脚本当前位于子目录中。您也不应该需要 api.php,因为它已经由 init.php 处理。将您的脚本移动到 WHMCS 根目录并注释掉该require('../includes/api.php');行应该有望解决空白页问题。

请注意:您提供的示例不显示正常的 WHMCS 客户端界面,也不检查用户是否已登录。如果您也需要该功能,您可以创建一个具有相同界面和功能的页面作为本机 WHMCS 客户区页面。以下是 WHMCS 在其创建客户区页面的指南中提供的示例代码的略微修改版本:

<?php

// Define WHMCS namespaces
use WHMCS\ClientArea;
use WHMCS\Database\Capsule;

// Initialize WHMCS client area
define('CLIENTAREA', true);
require __DIR__ . '/init.php';
$ca = new ClientArea();
$ca->setPageTitle('Your Page Title Goes Here');
$ca->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
$ca->addToBreadCrumb('mypage.php', 'Your Custom Page Name');
$ca->initPage();

// Uncomment to require a login to access this page
//$ca->requireLogin();

// Uncomment to assign variables to the template system
//$ca->assign('variablename', $value);

// Code to run when the current user IS logged in
if ($ca->isLoggedIn()) {
    $clientName = Capsule::table('tblclients')->where('id', '=', $ca->getUserID())->pluck('firstname');
    $ca->assign('clientname', $clientName);

// Code to run when the current user is NOT logged in
} else {
    $ca->assign('clientname', 'Random User');
}

// Setup the primary and secondary sidebars
Menu::addContext();
Menu::primarySidebar('announcementList');
Menu::secondarySidebar('announcementList');

// Define the template filename to be used (without the .tpl extension)
$ca->setTemplate('mypage');

// Display the contents of the page (generated by the Smarty template)
$ca->output();

推荐阅读