利用宽乐通信实现PHP网页收发短信
一直想为网站提供收发短信的功能。
最近学习了一下宽乐通信, 它是电信提供的一个服务,是华为公司开发的,以WebService方式提供了收发短信的开发接口, 用PHP调用这个接口就可以收发短信了。
使用宽乐通信前,要先开通一个帐号、密码(在电信公司开通,帐号即电话号码,开通时要声明是WebService方式的,否则不能用)
发短信是需要收费的,每条8分,接收免费。
宽乐通信有一个接口文档,用于二次开发的。在百度搜索 ”宽乐通信接口“ 即可找到
宽乐通信提供两种接口:
一种是SDK方式,即以Windows COM组件方式提供,用于编写Windows客户端程序
另一种是WebService方式,可用于网站开发。
所谓WebService,就是Web服务,是一种网络函数,可以看作另一个网站给你提供的函数,是一个网站调用另一个网站的功能的一种标准接口,具体来说,如果要调用其它网站的功能,向指定的URL发出一个POST请求,请求内容中包含的函数名和参数(XML格式), 响应结果中是函数返回值。 要详细了解WebService的数据格式, 具体看相关文档。
PHP5中提供了两个类 SoapClient (客户端, 使用WebService)和 SoapServer(服务器,提供WebService),可以很容易地调用WebService.
宽乐通信接口文档很长,简单讲一下收发短信的原理:
宽乐通信提供了一系列接口,其中与短信相关的有: Register, SendSMS两个接口
发短信的过程:
1, 首先通过WebService方式调用 Register 接口中的 函数 getRandom(), 取得宽乐通信平台下发的一个随机数rand
2, 然后调用 Register 接口中的 setCallBackAddr()函数实现登录
函数原型:String setCallBackAddr(String uc, String pw, String rand, String url);
参数: uc 是帐号
pw 是getRandom获取的随机数 rand+UC密码+UC密码 经过MD5加密后的字符串 (就是把rand与密码字符串两次相连后,进行MD5加密)
rand 是getRandom获取的随机数
url 是回调函数的url (这个回调函数是在接收短信时用到的,在这个URL上你要先实现一个WebService, 当你的宽乐通信帐号收到短信,宽乐通信平台将会调用你的WebService)
返回值是一个连接ID (connID), 这个ID用于后续发短信
3,然后,就可以通过调用 SendSMS 接口中的 sendSMS()函数发送短信了
函数原型:String sendSMS(String uc, String pw, String rand, String callee[], String isreturn, String cont, int msgid, String connID);
参数: uc 是帐号
pw 是getRandom获取的随机数 rand+UC密码+UC密码 经过MD5加密后的字符串 (就是把rand与密码字符串两次相连后,进行MD5加密)
rand 是getRandom获取的随机数
callee 是接收短信的号码,是一个字符串数组,可以放多个号码
isreturn 是否需要短信回执,这是宽乐通信很强的一个功能,当接收方已接收到短信,则宽乐通信平台会发送一个回执给短信发送方
cont 是短信内容,中文必须是gbk编码, 还要再进行base64编码
msgid 是短信的唯一编号, 你自己编的,收到短信回执中有这个msgid编号,这样就可以知道是哪条短信被接收了
connID 就是setCallBackAddr()函数返回的连接ID
收短信的过程:
调用 setCallBackAddr() 函数时要提供一个回调函数的url , 把这个URL指向你的网站,在这个URL上你要先实现一个WebService, 提供三个函数:echoOfSendSMS(), recvSMS(), NotifyStatus().
当收到短信时,宽乐通信平台会调用你指定的URL中的recvSMS()函数
当收到短信回执时,宽乐通信平台会调用你指定的URL中的echoOfSendSMS()函数
当状态变化时,宽乐通信平台会调用你指定的URL中的NotifyStatus()函数
你在自已的网站通过编写这三个webservice函数, 就可以实现接收短信了。
函数原型:String recvSMS(String caller, String time, String cont, String ucNum);
参数: caller是发送短信的号码,cont是短信内容(需要base64解码), ucNum是短信接受者号码
函数原型:void echoOfSendSMS(String ucNum, String cee, int msgid, int res, String recvt);
函数原型:void NotifyStatus(int eventID, String sessionID,int res,String para1);
原理讲完了,看一下PHP代码的实现:
开发语言:PHP 5.X
发送短信的代码如下:
$uc="02087XXXX1"; //宽乐通信帐号$pass="uu1XXXX56"; //宽乐通信密码$callBackURL='http://www.some.com/receive_sms.php'; //回调URL,如果不想接收短信,则这个URL可以随便写$urlRegister="http://202.105.212.146:8080/jboss-net/services/Register?wsdl"; //Register接口URL$client = new SoapClient($urlRegister); //生成一个SoapClient对象, 使用Register接口$rand = $client->__call("getRandom", array()); //调用getRandom()函数,取得随机数$pw = md5($rand.$pass.$pass); //计算PW值,$connID = $client->__call("setCallBackAddr", array($uc,$pw,$rand,$callBackURL));//调用setCallBackAddr()函数,取得连接IDif ($connID__call("sendSMS", array($uc,$pw,$rand,$callee,$needReceipt,$content,$msgId,$connID));if ($result==0) { //如果返回值为0,则发送成功 print "send OK";} else { print "send error, error code=".$result; //否则,返回值即是错误码}
程序注释已经很清晰了,补充说明:上述程序中的帐号、密码要更改为你的实际帐号密码
下面是,接收短信的php代码, 文件: receive_sms.php
/** * 接收短信回执 * 函数原型:void echoOfSendSMS(String ucNum, String cee, int msgid, int res, String recvt); * @param string $ucNum 发送方号码 * @param string $cee 接收方号码 * @param int $msgid 短消息编号,用于客户端匹配请求消息 * @param int $res 回执的结果(=1表示接收方成功接收短信, -1为系统异常;-12:系统超时;-92:短信无法送达对方) * @param string $recvt 短消息时间 * @return 无返回值 */function echoOfSendSMS($ucNum,$cee,$msgid,$res,$recvt){ $filename = "echo.txt"; $data = "ucNum=".$ucNum." cee=".$cee." msgid=".$msgid." res=".$res." recvt=".$recvt."\r\n"; file_put_contents($filename, $data, FILE_APPEND);}/** * 接收短信 * 函数原型:String recvSMS(String caller, String time, String cont, String ucNum); * @param string $caller 短信的发送者号码 * @param string $time 短信发送时间 * @param string $cont 短信内容(需要对短信内容进行BASE64解码) * @param string $ucNum 短信接受者号码(接收短信的UC号码) * @return string =0:成功返回, "http://www.some.com/receive_sms.php"));//这个uri要写本php文件所在的uri //注册三个函数$server->addFunction(array("echoOfSendSMS","recvSMS","NotifyStatus"));//启动soap server$server->handle();
程序注释很清晰,就是定义三个函数,生成一个WebService服务
程序中接收到短信后,只是存盘。你可以修改recvSMS()函数,增加自己想要的功能,比如:写数据库、回复短信等
为简化发送短信的过程,我编写了一个Sms类,对发短信过程进行了封装,使用者不需要懂宽乐通信接口,只要有宽乐通信帐号密码,发送短信只需要三句,例如:
$sms = new Sms(); //产生一个Sms类的对象if ($sms->login($account, $password)) //登录 $sms->send("139876XXXXX", "hello,sms"); //发短信
够简单了吧。
Sms类的源码文件 class.Sms.php 及接收短信的源码 receive_sms.php 可以在我的资源中下载,分享给大家
网站能收发短信后,就可以实现很多功能,比如:发送验证码、有人登录网站时可以发短信通知到你的手机、发短信就可以触发网站运行。
网站因此就活动起来了!!!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools