一。自定义了一个类
【比如在 App\Helpers中自定义一个类CloudPiece.php】
systemKey = "996d6c5c42cec80d5499c9fcbd756183"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
$this->operationKey = "1a1143af14792926772bc492154eeb43";//
// $mobile = "18010798215"; //请用自己的手机号代替
//$text="附近的经纪人小野,她/他想与您成为好友,正在**APP中等待您的回复 http://t.cn/ESicmsb 回T退订";
// $text = "经纪人小野,她/他在**同意了您的好友申请,快去APP中跟她/他愉快的聊天吧 http://t.cn/ESicmsb 回T退订";
$ch = curl_init();
/* 设置验证方式 */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
/* 设置返回结果为流 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* 设置超时时间*/
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
/* 设置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$this->ch = $ch;
}
/**
* 发送系统短信
* @param $text
* @param $mobile
* @return bool
*/
public function sendSystemSMS($text, $mobile)
{
$data = array('text' => $text, 'apikey' => $this->systemKey, 'mobile' => $mobile);
$json_data = $this->send($this->ch, $data);//"{"http_status_code":400,"code":5,"msg":"未找到匹配的模板","detail":"未自动匹配到合适的模板"}"
//记录日志
Log::info($json_data);
$this->storeLog($json_data,'system',$data);
if (json_decode($json_data)->code == 0) {
return true;
}
return false;
}
/**
* 发送营销短信
* @param $text
* @param $mobile
*/
public function sendOperationSMS($text, $mobile)
{
$data = array('text' => $text, 'apikey' => $this->operationKey, 'mobile' => $mobile);
$json_data = $this->send($this->ch, $data);//"{"http_status_code":400,"code":5,"msg":"未找到匹配的模板","detail":"未自动匹配到合适的模板"}"
//记录日志
Log::info($data);
Log::info($json_data);
$this->storeLog($json_data,'operation',$data);
}
/************************************************************************************/
//获得账户
function get_user($ch, $apikey)
{
curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
$result = curl_exec($ch);
$error = curl_error($ch);
$this->checkErr($result, $error);
return $result;
}
function send($ch, $data)
{
curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
$this->checkErr($result, $error);
return $result;
}
function tpl_send($ch, $data)
{
curl_setopt($ch, CURLOPT_URL,
'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
$this->checkErr($result, $error);
return $result;
}
function voice_send($ch, $data)
{
curl_setopt($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
$this->checkErr($result, $error);
return $result;
}
function notify_send($ch, $data)
{
curl_setopt($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
$this->checkErr($result, $error);
return $result;
}
function checkErr($result, $error)
{
if ($result === false) {
echo 'Curl error: ' . $error;
} else {
//echo '操作完成没有任何错误';
}
}
/**
* 记录日志
* @param $data
* @param string $name
* @param $request
*/
public function storeLog($data,$name = 'system',$request = '')
{
$path = 'logs/sms/'.$name.'.log';
$log = new Logger($name);
$log->pushHandler(
new StreamHandler(
storage_path($path),
100
)
);
if($request){
$log->addInfo(json_encode($request,JSON_UNESCAPED_UNICODE));
}
$log->addDebug($data);
}
}
二。且放入容器中后,—使用Laravel 的 Facade调用
Facades(读音:/fəˈsäd/ )为应用程序的 服务容器 中可用的类提供了一个「静态」接口。你不必 use 一大串的命名空间,也不用实例化对象,就能访问对象的具体方法。
Facade 其实是一个容器中类的静态代理,他可以让你以静态的方式来调用存放在容器中任何对象的任何方法。
只要定义一个类让他继承自 Illuminate\Support\Facades\Facade,并且实现一个抽象方法 getFacadeAccessor 即可。这个方法只要返回一个字符串,就是返回服务容器绑定类的别名。其实,通过源码可以知道,对象不一定要放到容器中,可以直接在这里返回也是可以的,
在App\Facades建一个类CloudPiece.php
三,通过providers进行服务提供者的注册
所有服务提供者都在配置文件 app.php 文件的 providers 数组中
app->bind('CloudPiece', function()
{
return new CloudPiece;
});
}
}
四。Facade 的启动
Facade 的启动引导是在 Illuminate\Foundation\Bootstrap\RegisterFacades 中注册的。
在config/app.php中设置相应的providers和aliases进行引用和命名
所有服务提供者都在配置文件 app.php 文件的 providers 数组中
'providers' => [
/*
* Laravel Framework Service Providers...
*/
App\Providers\CloudPieceServiceProvider::class,
],
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/
'aliases' => [
'CloudPiece' => App\Facades\CloudPiece::class,//云片短信
],