我已经在codeigniter中发送了一个短信发送代码。它运作成功。但实际上我想制作一个配置文件,以便我不会在每页上写下用户名,密码,senderid。
以下是我的代码。首先,我已经为短信发送做了一个库文件。
Sms.php
colorVariation
再次发送短信发送我在每个控制器中写代码就像这样。
<?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_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);
答案 0 :(得分:1)
在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');