菜单

PHP simple distribution to achieve multi-channel free switching

2017年11月22日 - PHPer

This article originally wants to write the PHP to realize the simple hook for you, can automatically complete some simple mechanical work. But taking into account the actual application is not necessary. So the article changed to write a simple distribution for PHP to achieve multi-channel free switching, below we specifically look at the implementation code.

/ / simple hook example

Class tool{

Public static function main ($class, $fun, $data = ‘) {

/ / pre public operation

$con = new $class;

$con-> $fun ($data);

/ / post public operation

}

}

Class a{

Function B ($data) {

Echo ‘I’m method b’;

}

}

Class c{

Function d ($data) {

Echo ‘I’m method d’;

}

}

/ / call hook

Tool:: Main (‘a’,’b’,’222′);

This package is intended to be implemented with hooks when encapsulating SMS channels,

It can send SMS (multi-channel) email, push and other messages automatically…

Later found that business needs are not so complex as imagined, development on the shelved….

T_T then uses a simple distribution method to implement it

Class Ar_Sms{

Const LANCHUANG = 1; / / Channel 1

Const ALIDAYU = 2; / / Channel 2

Private $type;

Private = array ($chuanglan_config / 1 channel configuration

‘api_send_url’=>’xxxx’,

‘api_balance_query_url’=>’xxxxx’,

‘api_account’=>’xxxx’,

‘api_password’=>’xxxxx’,

);

Private = array ($alidayu_config / 2 channel configuration

‘api_key’=>’xxxx’,

‘api_id’=>’xxxxx’,

‘api_send_url’=>’xxxxx’,

);

Public function __construct ($type=1) {

Switch ($type) {

Case self:: LANCHUANG:

$this-> type = $type; break;

Case self:: ALIDAYU:

$this-> type = $type; break;

Default:

$this-> type = false;

}

}

Sending method throws foreign /

Public function sendSms ($mobile, $msg) {

Switch ($this-> type) {

Case self:: LANCHUANG:

Return $this-> _sendCL ($mobile, $msg);

Case self:: ALIDAYU:

Return $this-> _sendAL ($mobile, $msg);

Default:

Return false;

}

}

/ / Channel 1 transmitting method

Private function _sendCL ($mobile, $msg, $needstatus =’false’, $extno =’) {

$postArr = array

‘account’=> $this-> chuanglan_config[‘api_account’],

‘pswd’=> $this-> chuanglan_config[‘api_password’],

‘msg’=> $msg,

‘mobile’=> $mobile,

‘needstatus’=> $needstatus,

‘extno’=> $extno

);

$result = $this-> _curlPost ($this-> chuanglan_config[‘api_send_url’], $postArr);

$result = $this-> _execResult ($result);

Return true: $result[1] $result[1] = = 0?;

}

/ / Channel 2 transmitting method

Private function _sendAL ($mobile, $msg) {

$postArr = array

‘id’=> $this-> alidayu_config[‘api_id’],

‘key’=> $this-> alidayu_config[‘api_key’],

‘msg’=> $msg,

‘mobile’=> $mobile,

);

$result = $this-> _curlPost ($this-> alidayu_config[‘api_send_url’], $postArr);

$result = $this-> _execResult ($result);

Return true: $result[1] $result[1] = = 0?;

}

//————- some public methods

/ * *

* processing return value \r\n segmentation

*

* /

Private function _execResult ($result) {

($result=preg_split /, \r\n]/, $result);

Return $result;

}

/ * *

* processing return value JSON

*

* /

Private function _jsonResult ($result) {

$result=json_decode ($result, true);

Return $result;

}

/ * *

* send HTTP requests through CURL

* @param string $url / / URL request

* @param array $postFields / / request parameters

* @return mixed

* /

Private function _curlPost ($url, $postFields) {

$postFields = http_build_query ($postFields);

$ch = curl_init ();

Curl_setopt ($ch, CURLOPT_POST, 1);

Curl_setopt ($ch, CURLOPT_HEADER, 0);

Curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

Curl_setopt ($ch, CURLOPT_URL, $url);

Curl_setopt ($ch, CURLOPT_POSTFIELDS, $postFields);

$result = curl_exec ($ch);

Curl_close ($ch);

Return $result;

}

}

$ob = new Ar_Sms (Ar_Sms:: ALIDAYU); / / Channel 1 to send a

$res = $ob-> sendSms (‘xxxxx’,’xxxxxx’);

Var_dump ($res);

$ob = new Ar_Sms (Ar_Sms:: LANCHUANG); / / Channel 2 to send a

$res = $ob-> sendSms (‘xxxxx’,’xxxxxx’);

Var_dump ($res);

Multi channel free switching through a simple distribution,

Because simply send so no abstract ~ orz further embarrassed

The implementation of the hook, the idea of a little big, specific implementation with elegant.. “There’s time for me to do research, and I’m going to throw a simple demo, and imagine -” multi channel, multi channel, multi-channel support, easy to expand

发表评论

电子邮件地址不会被公开。