background image

推荐 10 个非常实用的 PHP 代码片段

摘要:当使用 PHP

 

进行开发的时候,如果你自己收藏 了一些非常有用的方法或者代码片

段,那么将会给你的开发工作带来极大的便利。今天我们将介绍 10 个超级好用的 PHP 代
码片段,希望大家能够喜欢!
当使用 PHP

 

进行开发的时候,如果你自己收藏 了一些非常有用的方法或者代码片段,那

么将会给你的开发工作带来极大的便利。今天我们将介绍 10 个超级好用的 PHP 代码片段,
希望大家能够喜欢!
1.  

使用

 

     textmagic API

 

     发送消息

 

 

可能有的时候,你需要发送一些短信给你的客户,那么你绝对应该看看 textMagic。它提
供了非常简单的 API 来实现这个功能。但是不是免费的。
PHP

 

代码

// 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 ) 
2.  

通过

 

     IP

       判断来源

 

 

这是一个非常实用的代码片段,可以帮助你通过 IP 来判断访客来源。下面的方法通过接
收一个参数,然后返回 IP 所在地点。如果没有找到,则返回 UNKNOWN。
PHP

 

代码

function detect_city($ip) {  
 
        $default = 'UNKNOWN';  
 
        if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')  
            $ip = '8.8.8.8';  
 
                $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) 
Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';