L
L
Leonid2019-03-18 12:37:17
PHP
Leonid, 2019-03-18 12:37:17

How to pass parameters when calling a command via the Plesk REST API?

I want to set up an autoresponder for an existing e-mail address on the server via the Plesk REST API
. I try by calling the command:

function setRequest($url, $headers, $post_fields)
{		
  $post_fields = json_encode( (array) $post_fields );
  $ch = curl_init($url); 
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);    
  $response = curl_exec($ch); 
  curl_close($ch);
  return json_decode( $response, true );
}

var_dump( setRequest(
  'https://XXX.XXX.XXX.XXX:8443/api/v2/cli/autoresponder/call',
  array(
    'X-API-Key: c1986243-c4b7-0fc3-d543-67u9ad3a000a',
  ), 
  array(
    'params'=> array(
      '--update',
      'mail' => '[email protected]',
    ),
  )
) );

I get in response:
array(3) {
  ["code"]=>
  int(1)
  ["stdout"]=>
  string(0) ""
  ["stderr"]=>
  string(135) "Unknown option '[email protected]': /usr/local/psa/admin/plib/api-cli/autoresponder.php --update >>>[email protected]"
}

how to correctly pass parameters when calling (call) a specific command? I can't find anything in the documentation explaining the format of the JSON array.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid, 2019-03-18
@easycode

It turned out to be simple:

var_dump( setRequest(
  'https://XXX.XXX.XXX.XXX:8443/api/v2/cli/autoresponder/call',
  array(
    'X-API-Key: c1986243-c4b7-0fc3-d543-67u9ad3a000a',
  ), 
  array(
    'params'=> array(
      '--update',
      '-mail',
      '[email protected]',
      '-status',
      'true',
      '-subject',
      'Re: this subject text',
      '-text',
      'This is text email message!',
    ),
  )
) );

Parameters are written as array elements in order - as if they were in a CLI command, see https://docs.plesk.com/en-US/onyx/cli-linux/using-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question