客户端API调用无效,可与PHP一起使用

时间:2019-12-16 20:59:32

标签: php api cors axios whmcs

我正在尝试通过React应用程序内的axios调用API(WHMCS)。但是,每当我拨打电话时,都会出现以下错误:

  1. 进行OPTIONS调用(CORS)后,我得到以下信息: result=error;message=Invalid API Command

    但是,响应为200 OK:

    Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding
    Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
    Access-Control-Allow-Origin: *
    Access-Control-Max-Age: 1000
    Cache-Control: no-store, no-cache, must-revalidate
    Connection: keep-alive
    Content-Length: 40
    Content-Type: text/plain; charset=utf-8
    Date: Mon, 16 Dec 2019 20:44:34 GMT
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Keep-Alive: timeout=60
    Pragma: no-cache
    Server: nginx
    Set-Cookie: <redacted>
    X-Powered-By: <redacted>
    
  2. 无论何时发出第二个呼叫(POST),我都会得到以下信息: result=error;message=Authentication Failed

我在PHP中尝试了相同的调用,并且有效。

// JS

const ticketObject = {
      deptid: '1',
      subject: <redacted>,
      name: '<redacted>',
      message: <redacted>,
      email: '<redacted>',
      priority: 'Medium',
      markdown: true,
      responsetype: 'json',
      clientid: 1,
      username: <redacted>,
      password: <redacted>,
      action: 'OpenTicket',
      accesskey: <redacted>
    };

    const res = await axios({
      method: 'post',
      url: 'https://<redacted>/includes/api.php',
      data: ticketObject
    });

// PHP     

// API Connection Details
$whmcsUrl = "https://<redacted>/";

// For WHMCS 7.2 and later, we recommend using an API Authentication Credential pair.
// Learn more at http://docs.whmcs.com/API_Authentication_Credentials
// Prior to WHMCS 7.2, an admin username and md5 hash of the admin password may be used.
$username = "<redacted>";
$password = "<redacted>";
$secret   = "<redacted>";

// Set post values
$postfields = array(
    'username' => $username,
    'password' => $password,
    'action' => 'OpenTicket',
    'responsetype' => 'json',
    'accesskey' => $secret,
    'deptid' => '1',
    'subject' => 'test',
    'name' => 'text',
    'message' => 'comments',
    'email' => '<redacted>',
    'priority' => 'Medium',
    'markdown' => 'true',
);

// Call the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
$response = curl_exec($ch);
if (curl_error($ch)) {
    die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);

// Decode response
$jsonData = json_decode($response, true);

// Dump array structure for inspection
var_dump($jsonData);

结果:

array(4) {
  ["result"]=>
  string(7) "success"
  ["id"]=>
  int(1)
  ["tid"]=>
  string(6) "321914"
  ["c"]=>
  string(8) "DePC5zRT"
}

关于如何解决此问题的任何建议?

0 个答案:

没有答案