Create / Delete Forwarders Piped to File.

philipnewmannz

Registered
Jun 3, 2012
2
0
51
cPanel Access Level
Website Owner
Here is a little solution I've managed to come up with for 'creating' new forwarders using the API. It is a pretty simple process. I have tested this for me and it works.

PHP:
<?php
// include the class.
include_once "cPanel.php" ; 
// set up the vars
$host 		= 'yourhost.com'; 
$user 		= 'username';
$password	        = 'password'; 

// create the object
$xmlapi = new xmlapi($host, $user, $password); 
// set the port, this is needed as it will default to the whm.
$xmlapi->set_port(2083);
// turn debug mode on.
$xmlapi->set_debug(1); 
// set up param array.
$vars['domain']    = 'yourhost.com'; 
$vars['email']     = 'beer';  
$vars['fwdopt']    = 'pipe';
$vars['pipefwd']   = '/home/'.$user.'/public_html/parsingScript.php'; 

$result = $xmlapi->api2_query($user, 'Email', 'addforward', $vars); 
print_r($result);  
?>
The above code will add a the forwarder [email protected] to send all email to /home/username/public_html/parsingScript.php. This works nicely.

My problem now begins when I try and delete the forwarder.

Here is my code:

PHP:
<?php
// include the class.
include_once "cPanel.php" ; 
// set up the vars
$host 		= 'yourhost.com'; 
$user 		= 'username';
$password	        = 'password'; 

// create the object
$xmlapi = new xmlapi($host, $user, $password); 
// set the port, this is needed as it will default to the whm.
$xmlapi->set_port(2083);
// turn debug mode on.
$xmlapi->set_debug(1); 
// set up param array.
$vars['forwarder']   = '[email protected]=|/home/username/public_html/parsingScript.php'; 

$result = $xmlapi->api1_query($user, 'Email', 'delforward', $vars); 
print_r($result);  
?>
This code sends out the result as:

SimpleXMLElement Object ( [module] => Email [func] => delforward [type] => event [source] => internal [apiversion] => 1 [data] => SimpleXMLElement Object ( [result] => SimpleXMLElement Object ( ) ) [event] => SimpleXMLElement Object ( [result] => 1 ) [postevent] => SimpleXMLElement Object ( [result] => 1 ) [preevent] => SimpleXMLElement Object ( [result] => 1 ) )

Can anyone make any suggestions to what I might be doing wrong?
 

cPanelDavidN

Well-Known Member
Staff member
Dec 17, 2009
571
3
68
Houston, TX
cPanel Access Level
Root Administrator
Hi philipnewmannz,

Since you're making an API1 call, you need to use an ordinal array and not an associative array. The order that the arguments are presented in the API1 documentation is the order that you'd use in you code.

So for API1 Email::delforward, the 'forward' argument should be in index '0':
PHP:
$value_to_delete='[email protected]=|/home/username/public_html/parsingScript.php';
$vars = array($value_to_delete);     # or $vars[0] = $value_to_delete;

$result = $xmlapi->api1_query($user, 'Email', 'delforward', $vars);
Regards,
-DavidN