如何在codeigniter中集成sms网关

时间:2017-11-02 05:09:56

标签: php codeigniter curl sms

我有一个codeigniter项目。已有短信网关,如clickatell和twilio。我想在我的项目中再添加一个名为lifetimesms.com的sms网关。这是lifetimesms网关的代码。

<?php
  $username = 'username';
  $password = 'password';
  $to = '44xxxxxxxx';
  $from = 'Brand';
  $message = 'Test SMS from Lifetimesms.com';
  $url = "http://Lifetimesms.com/plain?username=".$username."&password=".$password."&to=".$to."&from=".urlencode($from)."&message=".urlencode($message)."";
//Curl Start
 $ch  =  curl_init();
 $timeout  =  30;
 curl_setopt ($ch,CURLOPT_URL, $url) ;
 curl_setopt ($ch,CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT, $timeout) ;
 $response = curl_exec($ch) ;
 curl_close($ch) ; 
 //Write out the response
 echo $response ;

 ?>

codeigniter应该怎么做?

1 个答案:

答案 0 :(得分:0)

为什么不直接将它添加到控制器文件(SMS.php) 像这样

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class SMS extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
    }



    public function index()
    {

  $username = 'username';
  $password = 'password';
  $to = '44xxxxxxxx';
  $from = 'Brand';
  $message = 'Test SMS from Lifetimesms.com';
  $url = "http://Lifetimesms.com/plain?username=".$username."&password=".$password."&to=".$to."&from=".urlencode($from)."&message=".urlencode($message)."";
//Curl Start
 $ch  =  curl_init();
 $timeout  =  30;
 curl_setopt ($ch,CURLOPT_URL, $url) ;
 curl_setopt ($ch,CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT, $timeout) ;
 $response = curl_exec($ch) ;
 curl_close($ch) ; 
 //Write out the response
 die($response);
}

}

然后就这样跑 http://xxxx.com/codeigniter/sms