WHMCS API添加用户以及产品示例

2015-10-22 Linyuting.cn 博客札记

    假设我们需要利用whmcs所提供的api来注册用户,并为该用户添加一款产品,可以利用以下代码。需要注意的是,使用whmcs api需要先到后台Setup -> General Settings -> Security -> API IP Access Restriction 添加ip白名单

    index.php

    

<?php

require_once 'class.php';
$whmcs = new whmcs('http://www.whmcs.com/whmcs/includes/api.php', '管理员', '管理员密码');

$results = $whmcs->exec(whmcs_function::create_user('新用户邮箱','新用户密码','自定义字段1,如果没有随便填即可'));//创建用户


if ($results["result"]=="success") {
# Result was OK!

	$results = $whmcs->exec(whmcs_function::add_order($results['clientid'], 1, '自定义字段1,如果没有随便填即可'));//添加订单

	if($results["result"]=="success"){

		$oid = $results["orderid"];
		$iid = $results["invoiceid"];

		$results = $whmcs->exec(whmcs_function::accept_order($oid));//激活订单
		$results = $whmcs->exec(whmcs_function::paid_invoice($iid));//将订单设置为已支付

		echo "SUCCESS..<br/>";
	}
	
} else {
	echo "The following error occured: ".$results["message"];
}

?>


    class.php

    

<?php

class whmcs{

	var $whmcsapi, $username, $password;

	function __construct($whmcsapi, $username, $password) {
		$this->whmcsapi = $whmcsapi;
		$this->username = $username;
		$this->password = $password;
	}

	function exec($postfields){
		$postfields['username'] = $this->username;
		$postfields['password'] = md5($this->password);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->whmcsapi);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 100);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
		$data = curl_exec($ch);
		curl_close($ch);
		$data = explode(";",$data);
		$results = array();
		foreach ($data AS $temp) {
			$temp = explode("=",$temp);
			if(count($temp)<2) continue;
			$results[$temp[0]] = $temp[1];
		}
		return $results;
	}

}


class whmcs_function{

	static function create_user($email, $password, $field1){
		$postfields = array();
		$postfields["action"] = "addclient"; 
		$postfields["firstname"] = "Client";
		$postfields["lastname"] = "Client";
		$postfields["companyname"] = "Client";
		$postfields["email"] = $email;
		$postfields["address1"] = "Beijing zhaoyang";
		$postfields["city"] = "Beijing";
		$postfields["state"] = "Beijing";
		$postfields["postcode"] = "100000";
		$postfields["country"] = "CN";
		$postfields["phonenumber"] = "12345678900";
		$postfields["password2"] = $password;
		$postfields["customfields"] = base64_encode(serialize(array("1"=>$field1)));
		$postfields["currency"] = "1";
		return $postfields;
	}

	static function add_order($cid, $pid, $field1){
		$postfields = array();
		$postfields["action"] = "addorder";
		$postfields["clientid"] = $cid;
		$postfields["pid"] = $pid;
		$postfields["billingcycle"] = "onetime";
		$postfields["customfields"] = base64_encode(serialize(array("1"=>$field1)));
		$postfields["paymentmethod"] = "alipayperson";
		return $postfields;
	}

	static function accept_order($oid){
		$postfields = array();
		$postfields["action"] = "acceptorder";
		$postfields["orderid"] = $oid;
		$postfields["autosetup"] = true;
		$postfields["sendemail"] = true;
		return $postfields;
	}

	static function paid_invoice($iid){
		$postfields = array();
		$postfields["action"] = "updateinvoice";
		$postfields["invoiceid"] = $iid;
		$postfields["status"] = "Paid";
		$postfields["paymentmethod"] = "alipayperson";
		return $postfields;
	}

}


参考文档:http://wiki.whmcs.com/

标签: whmcs

发表评论:

本站由emlog驱动 粤ICP备15042739号