几个月前,我写了一篇令人震惊的文章,讲述了如何使用 PHP 发送短信就像调用 mail() 一样简单。使用邮件方法的唯一缺点是您需要知道给定电话号码的运营商——这是一个让您打算向其投放广告的任何人询问的羞辱性问题。幸运的是,有一个简单、有效的 Web 服务可用,称为 TextMagic。 TextMagic 为多种语言(PHP、Perl、Python、Ruby、Java 等)提供 API,并且非常容易配置。
PHP
一旦您拥有 TextMagic 帐户并下载了 TextMagic 提供的 API 帮助程序,您需要做的就是创建一些自定义代码来发送您的短信:
// Include the TextMagic PHP lib require('textmagic-sms-api-php/TextMagicAPI.php'); // Set the username and password information $username = 'myusername'; $password = 'mypassword'; // Create a new instance of TM $router = new TextMagicAPI(array( 'username' => $username, 'password' => $password )); // Send a text message to '999-123-4567' $result = $router->send('Wake up!', array(9991234567), true); // result: Result is: Array ( [messages] => Array ( [19896128] => 9991234567 ) [sent_text] => Wake up! [parts_count] => 1 )
send
调用的结果是一组有关消息发送的信息。稍后要检查发送状态,您只需调用 messageStatus
方法:
// Get the message send status by received ID $result = $router->messageStatus(array(19896128)); // result: Array ( [19896128] => Array ( [text] => Wake up! [status] => d [created_time] => 1325261093 [reply_number] => 447624800500 [completed_time] => 1325261120 [credits_cost] => 1 ) )
如果您想查看电话号码的状态以及向该号码发送短信的信用费用,您可以调用 checkNumber:
// Get the message number information $result = $router->checkNumber(array(9991234567)); // result: Array ( [16083359316] => Array ( [price] => 1 [country] => US ) )
TextMagic 是一项非常棒的服务。该帐户易于创建,API 简单,最重要的是,无需知道收件人的运营商!我发现的一个缺点是我无法控制“发件人”姓名或电话号码,因此文本来自静态号码。除此之外,TextMagic 非常出色!
更新:您可以在您的 TextMagic 帐户中更改“发件人”地址。但是,该功能在美国或墨西哥不可用。