Home Backend Development PHP Tutorial 运用PhP免费发送定时短信(用于天气预报、定时提醒、短信祝福等等)

运用PhP免费发送定时短信(用于天气预报、定时提醒、短信祝福等等)

Jun 13, 2016 pm 01:17 PM
api cron http nbsp php

使用PhP免费发送定时短信(用于天气预报、定时提醒、短信祝福等等)

使用PhP免费发送定时短信

 

背景:老妈年纪大了,文化程度不高,但是需要每天了解天气信息才能下地工作,也就无法通过网络获取天气,只能通过电视。于是寻思着自己抓取网络上的天气信息,通过手机短信发送给老妈。

问题:自己不可能每天定时给老妈发送天气短信,现有的天气预报订阅短信又是要钱的。

解决:

  •  免费发送短信到手机

         方案1:使用飞信API

         由于飞信目前没有开发平台,现有API都是第三方开发,貌似是通过破解WAP飞信或者3G飞信,模拟浏览器的行为来发短信。另外Google code上有开源的php飞信发送类php-fetion(http://code.google.com/p/php-fetion/)。这些飞信接口方便了定时短信的开发。

         唯一的缺点就是第三方的API不稳定,也有飞信账号泄露的风险。

         此处,我使用http://3.ibtf.sinaapp.com/提供的飞信API进行短信发送。该站本身挂在新浪SAE上,源代码可以在http://www.oschina.net/code/snippet_202258_9754找到(里面提供下载地址)。

         该API只需提供发送者手机号,密码,接受者手机号,消息内容作为url参数即可。唯一需要注意的是中文的乱码问题(使用gbk而不是utf-8编码,直接输入中文乱码,输入中文的gbk编码手机接收正常),可以使用iconv函数或者加上参数”&u=1”解决。

         举例:调用免费API

http://2.ibtf.sinaapp.com/?phone=手机号&pwd=飞信密码&to=接收手机号(必须为飞信好友或自己的手机号)&msg=消息内容&u=1(解决乱码问题)     

        

         方案2:使用139邮箱的短信提醒功能

         139邮箱也是中国移动的一项业务,提供了邮件短信提醒的功能。只要在邮箱里面设置邮件到达通知就OK了,除了彩信,其他的长短信,普通短信,免提短信都是免费的。注意,时间久了,发件箱和收件箱都要清除一下,以免占用过多空间而影响正常使用。

另外,只需要编写一个简单的邮件发送的php脚本。而新浪SAE也提供了此Mail服务。使用Mail服务里的函数,可以快速发送邮件,代码如下:

$mail = new SaeMail();

$ret = $mail->quickSend(

'收件人@139.com' ,

'这是主题' ,

'这是正文' ,

'这是发件人邮箱' ,

'这是发件人密码' ,

'这是smtp服务host' ,

[这是smpt服务端口] );

 

//发送失败时输出错误码和错误信息

if ($ret === false)

var_dump($mail->errno(),$mail->errmsg());

?>

详细信息请参考Mail服务API文档http://apidoc.sinaapp.com/sae/SaeMail.html。

效果如下:

 



  •  定时发送功能

短信成功发送后,就需要定时了。还好新浪SAE提供了Cron服务。Cron服务是SAE为开发者提供的分布式定时服务,用来定时触发开发者的特定动作,满足比如定时计算排行榜等需求。Cron的设置是通过App的config.yaml来实现的,只要用户在App目录下的config.yaml里按照我们的提供的语法格式进行配置,部署后就能生效。

举例:详细教程参考API文档http://sae.sina.com.cn/?m=devcenter&catId=195

增加Cron

   编辑saetest/1/config.yaml,增加cron段,config.yaml示例如下:

   name: saetest

   version: 1

   cron:

        - description: cron test

        url: mycron/test.php

        schedule: $2 day of october 19:00

        timezone: Beijing

        - description: another cron test

        url: mycron/another_test.php

        schedule: every 10 mins

        timezone: Beijing

    上面的示例描述了两个定时任务:每年10月2日19点执行mycron/test.php;每隔10分钟执行mycron/another_test.php

  • PHP天气抓取

网上有很多地方提供天气信息的,比如新浪,中国天气网等,也有相关API和开源的代码。这个比较简单就不细说了。当然上面提到的飞信API站点http://3.ibtf.sinaapp.com/的源码里也有关于天气抓取的代码,可以借鉴。

  •  实现效果图




1楼jaylongli5小时前
其实有软件自带天气通知功能的,比如飞信就可以设置定时天气通知。
Re: xiangshimoni3小时前
回复jaylonglin好像是企业版的功能吧,要收费
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles