toplan / phpsms Goto Github PK
View Code? Open in Web Editor NEW:iphone::rocket:Probably the most intelligent, elegant sms send library in php
License: MIT License
:iphone::rocket:Probably the most intelligent, elegant sms send library in php
License: MIT License
composer require toplan/phpsms:~1.8
云片支持模板发送,请求更新一下介绍,谢谢。具体可以参考我们官网介绍:https://www.yunpian.com/
腾讯云最近也出了短信API
https://www.qcloud.com/product/sms.html
官网:
http://www.alidayu.com/?spm=a3142.7791109.1.1.6AQ1vP
阿里云的东西应该比较靠谱 稳定,希望可以整合。@toplan
namespace App\Library;
use Toplan\PhpSms\Agent;
use Toplan\PhpSms\ContentSms;
class LanzSMSAgent extends Agent implements ContentSms
上面我自定义代理类的,phpsms.php配置文件是:
'scheme' => [
'LanzSMS'
],
'agents' => [
//自定义代理
'LanzSMS' => [
'userId' => 'xx',
'account' => 'xx',
'password' => 'xxx',
]
然而程序是用这个$className = "Toplan\PhpSms\{$name}Agent";,如何在phpsms.php定义agentClass
我使用lumen做为API核心处理系统,现在想用这个类库进行短信发送,但是我发现一个问题,就是PhpSmsServiceProvider类里面返回的Sms类是没有初始化的,然后整个文档都是说用门面调用,但是lumen是默认禁用门面的,我尝试用app("PhpSms")的形式加载Sms类,但是因为没有初始化Sms,所以基本上不可用。希望后面能适配一下lumen的使用。
https://www.253.com/
这家的方便接入吗?
和原来的其他代理放一个目录?这样好吗
v2目前规划:
Toplan\PhpSms\Agent
抽象类,把抽象类中发送相关的方法再抽离出来,定义成一系列接口,需要实现模版短信的实现TemplateSms
接口,需要实现内容短信的实现ContentSms
接口,语音也一样,以此类推。Message
, 规划的子类有Sms,TemplateSms,ContentSms,Voice,TemplateVoice,FileVoice,CodeVoiceSms::to()->content()->template()->data()->send(); //短信全能类
TemplateSms::to()->template()->data()->send(); // TemplateSms非全能,没有content方法
Dispacher
来处理:Dispacher::scheme(...);
Dispacher::config(...);
Dispacher::queue(...);
...
如题~
composer require 'toplan/phpsms:~1.6.0',win10环境下报错。单引号改成双引号或是不加引号则通过。
是这样的 在代码中 使用的是alidayu的scheme, 可以正常使用, 在写测试用例的时候 使用
Config::set('phpsms.scheme', ['Log']);
指定scheme 默认为Log 避免真的发送短信 但是问题来了 在代码环境中使用alidayu代码都能正常运作 甚至注释了Config这段代码 都可以正常运行,唯独在上述代码运作时 返回的success结果为false 这是为什么呢
附上日志及代码
使用send
发送回来的结果dump后结果是
array:3 [
"success" => false
"time" => array:2 [
"started_at" => "0.12025000 1504518904"
"finished_at" => "0.12125500 1504518904"
]
"logs" => array:1 [
0 => array:4 [
"driver" => "Log"
"time" => array:2 [
"started_at" => "0.12028900 1504518904"
"finished_at" => "0.12123700 1504518904"
]
"success" => false
"result" => array:2 [
"info" => null
"code" => 0
]
]
]
]
发送的代码
$result = PhpSms::make()
->template($template)
->to($phone)
->data($data)
->send();
$template
为 ['Alidayu' => env('SMS_ALI_VERIFY_TEMPLATE')]
$data
为 compact('code')
[Toplan\PhpSms\PhpSmsException]
Modify the dispatch scheme failed for [Log] agent, because the task system has already started.
你好,我现在正用阿里大于测试,可以判断发送失败或成功,但是我想记录返回的详细消息到数据库中。以方便排错之类。
请给一个返回结果的数据结构?
如何记录具体错误代码、详细错误消息?
如果使用其他服务商,返回的结果也是一样吗?
https://github.com/toplan/task-balancer 这里提到了$result结构,但是并没有关键细节,不太明白。
使用YunTongXun Agent时,该Agent调用了 CCPRestSmsSDK,但传入的$filename为 null,导致报错,
错误信息:fopen(): Filename cannot be empty
代码在:
https://github.com/toplan/phpsms/blob/master/src/phpsms/lib/CCPRestSmsSDK.php#L37
Problem 1
- Installation request for toplan/phpsms ~1.8.0-beta -> satisfiable by toplan/phpsms[1.8.0-beta.1].
- toplan/phpsms 1.8.0-beta.1 requires toplan/task-balancer ~0.5.0-beta -> satisfiable by toplan/task-balancer[0.5.0-beta.1] but these conflict with your requirements or minimum-stability.
如 Luosimao
需要对clearnScheme重写,并实现task-balancer的热更新功能
{ "tel": { //如需使用国际电话号码通用格式,如:"+8613788888888" ,请使用sendisms接口见下注 "nationcode": "86", //国家码 "mobile": "13788888888" //手机号码 }, "type": 0, //0:普通短信;1:营销短信(强调:要按需填值,不然会影响到业务的正常使用) "msg": "你的验证码是1234", //utf8编码 "sig": "30db206bfd3fea7ef0db929998642c8ea54cc7042a779c5a0d9897358f6e9505", //app凭证,具体计算方式见下注 "time": 1457336869, //unix时间戳,请求发起时间,如果和系统时间相差超过10分钟则会返回失败 "extend": "", //通道扩展码,可选字段,默认没有开通(需要填空)。 //在短信回复场景中,腾讯server会原样返回,开发者可依此区分是哪种类型的回复 "ext": "" //用户的session内容,腾讯server回包中会原样返回,可选字段,不需要就填空。 }
注意电话号码那一部分。是tel:{} 不是接收固定号码
同时,在日志查看,您其实是有用户消息服务的batchSendSms接口发送的,并且在2017-06-23 14:58:26有成功。建议您继续调整使用batchSendSms接口发送。
2017年6月22日起,阿里大于入驻用户使用的皆为阿里云云通信产品,原大于接口不可使用.
现在短信发送成功判断用的是
$result->statusCode === 0
但实际上,云通讯的状态码是 000000 。所以现在短信发送成功了,但组件返回结果 $result['success'] 为 false
ps: 云通讯和云之讯的接口几乎一样的,只是某些接口路径有一点点不同,如果有时间的话,可以考虑改成共用一个 lib .
如果你们做自定义代理器,要注意库里的代码有一处有问题,
Toplan\PhpSms\Sms.php
的289行
开始
代码是:
if (!empty($options)) {
self::$agents[$name] = new ParasiticAgent($config, $options);
} elseif (class_exists($className)) {
self::$agents[$name] = new $className($config);
} else {
throw new PhpSmsException("Not support agent `$name`.");
}
这样如果你在phpsms.php
里的scheme
里以数组的形式设置了你的自定义代理器并加了weight
,
比如
'scheme' => [
App\agents\AAA\Agent::class => [
'20',
'agentClass' => App\Libs\AAA\Agent::class
],
App\agents\BBB\Agent::class => [
'10 backup',
'agentClass' => App\Libs\BBB\Agent::class
]
]
那么这两个Agent经过那段代码后,就跑ParasiticAgent
去了。
所以,要不就不要有weight
,这样的后果 我不知道会不会造成没有权重的特性了,
我也没多看其它代码,就把elseif
改了一下
if (!empty($options)) {
self::$agents[$name] = new ParasiticAgent($config, $options);
}
if (class_exists($className)) {
self::$agents[$name] = new $className($config);
} else {
throw new PhpSmsException("Not support agent `$name`.");
}
希望遇到同样问题的同学能看到,着实弄了我半天。醉了!
允许推入队列,并自定义队列实现逻辑(与队列系统松散耦合)。
比如 将队列存储到数据库,然后定时任务发送什么的. 要求比较过分, 不知道能否考虑.
'Alidayu' => [
'sendUrl' => 'http://gw.api.taobao.com/router/rest'
];
由于审核原因,云片网的营销短信需要申请营销账号,因此会有两个apikey.
通过账号类型动态设置apikey没什么问题,但是有更好的解决方案么?
我自定义了YunPianAgent,想在Agent这边完成apikey的切换.
如果使用SMSManager,传递到Agent的业务数据已经被框架限制了.
` public function sendSms($to, $content, $tempId, array $data)
{
$this->sendContentSms($to, $content);
}
public function sendContentSms($to, $content)`
由于短信接口被调用的地方比较多,封装到Agent的话,只需要
正常使用SMSManager就可以了.
使用最新的laravel版本,使用本扩展,在执行php artisan
时报错
[ErrorException] Illegal offset type in unset
debug后发现是这段代码出错,
$this->app->singleton([
'Toplan\\Sms\\SmsManager' => 'laravel-sms',
], function ($app) {
$token = $app->request->header('access-token', null);
if (empty($token)) {
$token = $app->request->input('access_token', null);
}
$input = $app->request->all();
return new SmsManager($token, $input);
});
猜测应该是新版本laravel的singletion
绑定时不能再使用数组,
代码更改为如下后就正常了,
$this->app->singleton('Toplan\\Sms\\SmsManager', function ($app) {
$token = $app->request->header('access-token', null);
if (empty($token)) {
$token = $app->request->input('access_token', null);
}
$input = $app->request->all();
return new SmsManager($token, $input);
});
希望可以发布一个新版本修复一下,谢谢
我把phpsms升级到1.8.0后云片发送短信时报错,我查看了下错误,应该是在Agent.php中curl()处循环读取$opts时报错,
foreach ($opts as $key => $value) {
curl_setopt($ch, $key, $value);
}
我将其改成
curl_setopt($ch, CURLOPT_SSL_POST,$opts[3]);
curl_setopt($ch, CURLOPT_SSL_HTTPHEADER,$opts[1]);
curl_setopt($ch, CURLOPT_SSL_POSTFIELDS,http_query_bulid($opts[0]));
curl_setopt($ch, CURLOPT_SSL_HTTPURL,$opts[4]);
以后就正常发送云片短信了。
受限于网络或者运营商内部问题,即便在配置了比例、备份策略后,仍然要尽可能提高短信发送成功率。建议:
1.增加daemon程序,自动判断短信发送成功率,动态调整通道开关。能够自动开和关。甚至可以通过ui或者命令行,在不改代码的情况下,调整发送比例。
2.短信字符拆分策略,每家大同小异,但是依然可以配置是否在提交到短信服务商之前切分,还是直接发送长短信,因为各家针对拆分后的回调机制是不同的。
3.短信发送需要永久化保存,以便能够查询发送结果。
4.对于上行短信,需要配置是否支持扩展码,以自动对于下发内容。
以上4点是我们公司目前短信策略,供参考。
出现1004错误
[{"driver":"Qcloud","time":{"started_at":"0.33102900 1507352277","finished_at":"0.60767200 1507352277"},"success":false,"result":{"info":"{\"result\":1004,\"errmsg\":\"package format errorcannot get \/tel\/mobile\"}","code":1004}}]
$this->app->singleton([
'Toplan\Sms\SmsManager' => 'laravel-sms',
], function ($app) {
$token = $app->request->header('access-token', null);
if (empty($token)) {
$token = $app->request->input('access_token', null);
}
$input = $app->request->all();
return new SmsManager($token, $input);
});
1.8.0版本发送短信: {"success":false,"time":{"started_at":"0.47898600 1498094446","finished_at":"0.53329300 1498094446"},"logs":[{"driver":"Alidayu","time":{"started_at":"0.47902900 1498094446","finished_at":"0.53327600 1498094446"},"success":false,"result":{"info":"{\"code\":25,\"msg\":\"Invalid signature\",\"request_id\":\"zlycsgi622v2\"}","code":25}}]}
固定版本到1.7.2 发送短信成功: {"success":true,"time":{"started_at":"0.53952600 1498095191","finished_at":"0.18113300 1498095192"},"logs":[{"driver":"Alidayu","time":{"started_at":"0.53955100 1498095191","finished_at":"0.18108200 1498095192"},"success":true,"result":{"info":"{\"err_code\":\"0\",\"model\":\"108284793846^1111232052740\",\"success\":true}","code":"0"}}]}
望解决回复
建议增加阿里云短信平台
一般会有多种情况会使用到验证码,登陆/注册/重置,针对某些场景会不希望同一个验证码能做其他功能的验证
自定义代理器类使用 Toplan/PhpSms 命名空间
对于Laravel项目 ,在重新执行 php artisan optimize ,生成的 autoload_classmap.php
中也不会出现自定义的类的相关映射.
$className = "Toplan\\PhpSms\\{$name}Agent";
class_exists($className);
Agent.php
与 QcloudAgent.php
文件中里都是存在 formatMobile()
,冲突导致无法获得手机号,短信发送失败。
以防恶意调用接口,造成不必要的损失。
1.针对一个ip,限制次数。
2.针对一个手机号码,限制次数
另外,对于发送验证码的情况,我们要限制尝试次数。
在配置文件写的,配置在"agents"项,如何正确获取?
云片为什么不用 curl
发送?
提供了beforeDriverRun 但是beforeDriverRun中无法标记当前agent 失效.
比如, 目前发送短信会有某个时间内的次数限制,当之前已经使用某agent进行发送了.之后又多次请求.
按理说,发送不成功应该不会计费, 但是目前的短信网关一般都是不发送,但是进行计费的.
如果在beforeDriverRun 时根据当前agent的判断, 进行当前agent跳过或直接标记agent错误,那就会好很多了.
Toplan\PhpSms\JuHeAgent 方法genResult 使用if ($result['error_code'] === '0')判定,实际返回数据为0,所以永远发送失败
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.