Use PHP to send timed messages free of charge
Background: Mom is old, not highly educated, but need to know the weather information every day to work in the fields, and can not get the weather through the network, only through television. So I thought about crawling the weather information on the Internet and sending it to my mother by SMS.
Problem: It is not possible to send the weather message to my mother every day, the existing weather forecast to subscribe to SMS is Money.
Solve:
Send SMS to mobile phone for free
Scenario 1: Using the fly-through API
Because there is no development platform, the existing APIs are Third-party development, seemingly by cracking WAP flying or 3G flying, simulate the behavior of the browser to send text messages. In addition, Google Code has open source PHP fly Letter Send class Php-fetion (http://code.google.com/p/php-fetion/). These fly-through interface facilitates the development of timed SMS.
The only drawback is that the Third-party API is not stable, but also the risk of flying letter account leakage.
Here, I use http://3.ibtf.sinaapp.com/provided by the Flying Letter API to send text messages. The station itself hangs on the Sina SAE, the source code can be found in the http://www.oschina.net/code/snippet_202258_9754 (inside provide download address).
The API simply provides the sender's cell phone number, the password, the recipient's cell phone number, and the message content as a URL parameter. The only attention is the Chinese garbled problem (using GBK instead of UTF-8 encoding, direct input Chinese garbled, input Chinese GBK code phone to receive normal), you can use the Iconv function or add parameters "&u=1" to solve.
For example: Invoke the Free API
http://2.ibtf.sinaapp.com/?phone= mobile phone number &pwd= Fly letter password &to= receive mobile phone number (must be flying friends or their own mobile phone number) &msg= message content &u=1 (solve garbled problem)
Option 2: Use the SMS alert function of 139 mailboxes
139 mailbox is also a mobile business in China, providing the function of email message reminder. As long as the mail in the mailbox to set up the arrival of the notification OK, in addition to MMS, other letters, ordinary text messages, hands-free text messages are available. Note that time is long, the Outbox and Inbox should be cleared, so as not to occupy too much space and affect the normal use.
In addition, you only need to write a Simple mail-sent PHP script. And the Sina SAE also provides this mail service. Using the functions in mail service, you can quickly send mail with the following code:
<?php
- $mail = new Saemail ();
- $ret = $mail->quicksend (
- ' Recipient @139.com ',
- ' This is the theme ',
- ' This is the text ',
- ' This is the sender's mailbox ',
- ' This is the sender's password ',
- ' This is the SMTP Service host ',
- [This is the Smpt service port]);
- Output error code and error message when sending fails
- if ($ret = = False)
- Var_dump ($mail->errno (), $mail->errmsg ());
- ?>
Please refer to the Mail Service API documentation http://apidoc.sinaapp.com/sae/SaeMail.html for more information.
The effect is as follows:
Timed Send function
After the message is sent successfully, it needs to be timed. Fortunately, the Sina SAE provides a cron service. The cron service is a distributed, timed service provided by the SAE for developers to periodically trigger specific actions of the developer to meet the needs of such as the timing list. Cron settings are implemented through the app's Config.yaml, and as long as the user is configured in the Config.yaml in the app directory according to our provided syntax format, it will take effect after deployment.
Example: detailed tutorial Reference API documentation http://sae.sina.com.cn/?m=devcenter&catId=195
Add cron
Edit Saetest/1/config.yaml, add cron segments, Config.yaml examples are as follows:
- Name:saetest
- Version:1
- Cron:
- -Description:cron Test
- url:mycron/test.php
- Schedule: $ Day of October 19:00
- Timezone:beijing
- -Description:another cron Test
- url:mycron/another_test.php
- Schedule:every mins
- Timezone:beijing
The above example describes two timed tasks: mycron/test.php every October 2 19 o'clock, performing mycron/another_test.php every 10 minutes
PHP Weather Crawl
There are many places on the Internet to provide weather information, such as Sina, China Weather Network, there are also relevant APIs and open source code. This is more simple than to elaborate. Of course, the above mentioned the letter of the API site http://3.ibtf.sinaapp.com/The source code also has about the weather to crawl, you can learn from.
Implementation of the effect diagram