Jump to content

Adding custom (external) api functions


aggrosoft

Recommended Posts

  • 3 weeks later...

You did not understand the question, anyways - it's easy. You can just add your own php files to the api folder and they will be used as the action:

 

<?php

//File includes/api/adddownload.php

use WHMCS\Database\Capsule;

if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}

function getParams($vars) {
$param = array('action' => array(), 'params' => array());
if (isset($vars['cmd'])) {
	//Local API mode
	$param['action'] = $vars['cmd'];
	$param['params'] = (object) $vars['apivalues1'];
	$param['adminuser'] = $vars['adminuser'];
} else {
	//Post CURL mode
	$param['action'] = $vars['_REQUEST']['action'];
	unset($vars['_REQUEST']['username']);
	unset($vars['_REQUEST']['password']);
	unset($vars['_REQUEST']['action']);
	unset($vars['_REQUEST']['accesskey']);
	$param['params'] = (object) $vars['_REQUEST'];
}
return (object) $param;
}
try {
// Get the arguments
$vars = get_defined_vars();
$params = getParams($vars);
$data = $params->params;

$pid = Capsule::table('tblproducts')->where('moduleid', $data->module)->value('id');

if($pid){
	$downloadId = Capsule::table('tbldownloads')
	    ->insertGetId([
	    	'category' => isset($data->category) ? $data->category : 2,
	    	'type' => isset($data->type) ? $data->type : 'zip',
	    	'title' => $data->title,
	    	'description' => $data->description,
	    	'location' => $data->file,
	    	'clientsonly' => isset($data->clientsonly) ? $data->clientsonly : 1,
	    	'created_at' => new DateTime(),
	    	'updated_at' => new DateTime(),
	    	'productdownload' => isset($data->productdownload) ? $data->productdownload : 1
	    ]);

	Capsule::table('tblproduct_downloads')->insert([
		'product_id' => $pid,
		'download_id' => $downloadId
	]);

	//Cleanup old downloads, keep maximum of 5 Versions
	/*$count = Capsule::table('tblproduct_downloads')->where([
		'product_id' => $pid
	])->count();

	if($count > 5){

	}*/
}else{
	throw new Exception('Module with ID '.$data->module.' not found');
}

$apiresults = array("result" => "success", "message" => "Download added successfully!");
} catch (Exception $e) {
$apiresults = array("result" => "error", "message" => $e->getMessage());
}

Link to comment
Share on other sites

  • 1 year later...

Just wanted to add a bit of context here, what is discussed here is a method to add code server side that can be triggered remotely. For me an API can be understood as a promise between the developer using it and then developer providing it to communicate and respond in specific ways. We work hard to preserve backwards compatibility with our APIs. We are not making that kind of a promise and in fact this will not work with our API Credential Roles functionality. If you want to implement a URL that can run custom code, I would recommend using an addon module with the admin area output or client area output functionality:

https://developers.whmcs.com/addon-modules/admin-area-output/

https://developers.whmcs.com/addon-modules/client-area-output/

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated