如何使SMS发送单独的配置文件



我已经在CodeIgniter中完成了SMS发送代码。它成功地工作了。但是实际上,我想制作一个配置文件,以便我不会在每个页面上编写用户名,密码,senderid。

这是我下面的代码。首先,我完成了一个用于SMS发送的库文件。

sms.php

<?php
class SMS
{
    function SendSMS($url)
    {
        if(function_exists('curl_init'))
        {
            $ch = curl_init(); 
            $timeout = 60; 
            curl_setopt($ch, CURLOPT_URL, $url); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
            curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0); 
            curl_setopt($ch, CURLOPT_POST, true); 
            curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
            $data = curl_exec($ch); 
            if($data === FALSE){ 
                throw new Exception(curl_errno($ch)); 
            } 
            curl_close($ch); 
            return $data;       
        }
        else
        {
            return false;
        }
    }
}
?>

再次,对于SMS发送我在每个控制器中写代码。

$sms_username = "GAPSMS";
$sms_password = "GAPSMS";
$sms_senderid = "GAPSMS";
$sms_mobile = $mobile;
$sms_message  = urlencode('Your One Time Password for transaction is: '.$otp);
$sms_api = "http://sendsms.sandeshwala.com/API/WebSMS/Http/v1.0a/index.php?username=$sms_username&password=$sms_password&sender=$sms_senderid&to=$sms_mobile&message=$sms_message&reqid=1&format={json|text}";
$this->sms->SendSMS($sms_api);

这是我想以配置文件编写的代码以下的多种方式,以便我可以写一次,然后可以在每个控制器中使用以发送SMS。

$sms_username = "GAPSMS";
$sms_password = "GAPSMS";
$sms_senderid = "GAPSMS";

更新

在CodeIgniter中,库可以具有自己的配置文件。让我们更改您的库:

<?php
class Sms
{
    private $_username = 'GAPSMS'; // default value
    private $_password = 'GAPSMS'; // default value
    private $_senderid = 'GAPSMS'; // default value
    /**
     * Class constructor so the config
     * file is loaded.
     */
    public function __construct($config = array())
    {
        if ( ! empty($config))
        {
            foreach ($config as $key => $val)
            {
                $this->{"_".$key} = $val;
            }
        }
    }
    function send($mobile, $message)
    {
        if(function_exists('curl_init'))
        {
            $url = 'http://sendsms.sandeshwala.com/API/WebSMS/Http/v1.0a/index.php?username='.$this->_username.'&password='.$this->_password.'&sender='.$this->_senderid.'&to='.$mobile.'&message='.$message.'&reqid=1&format={json|text}';
            $ch = curl_init(); 
            $timeout = 60; 
            curl_setopt($ch, CURLOPT_URL, $url); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
            curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0); 
            curl_setopt($ch, CURLOPT_POST, true); 
            curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
            $data = curl_exec($ch); 
            if($data === FALSE){ 
                throw new Exception(curl_errno($ch)); 
            } 
            curl_close($ch); 
            return $data;       
        }
        else
        {
            return false;
        }
    }
}

然后创建您放置的配置文件application/config/sms.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['username'] = 'GAPSMS';
$config['password'] = 'GAPSMS';
$config['senderid'] = 'GAPSMS';
// End of file.

现在,只要库加载库,并且在设置了值时加载了配置文件,一切都应该正常工作。其余代码将是:

// Load the library and simply pass mobile number and message.
$this->load->library('sms');
$data = $this->sms->send('0123456789', 'Hello, this is the message');

最新更新