Run another function in controller class - Background process

#11
[eluser]tonanbarbarian[/eluser]
if we make an instance of CI in the constructor of the library we can do anything

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

class sms {
  var $CI = null;

  function sms() {
    if ($this->CI===null)
      $CI =& get_instance();
  } // sms()

  function background() {
    $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
    if (!$fp) {
      log_message('error', 'Unable to connect to server');
      return;
    }
    $request = "GET /SendSMS/index HTTP/1.1\r\n"
      ."Host: www.example.com\r\n"
      ."Connection: Close\r\n\r\n";
    fwrite($fp, $request);
    fclose($fp);
  } // background()

  function query() {
    $this->CI->db->query();
    ...
  }
} // class sms

So if you want to call anything you could in the controller use $this->CI->... rather than $this->...

#12
[eluser]cinewbie81[/eluser]
Hi tonanbabarian,

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30) return me true ..
and now im confuse where should i write code / function to send the sms ??

#13
[eluser]tonanbarbarian[/eluser]
This library is a way of calling code in another controller by requesting the page in a similar fashion to a browser would.
So create the code you were going to create in the SendSMS controller

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
opens a connection (like tying in location bar of the browser) to the site www.example.com

$request = "GET /SendSMS/index HTTP/1.1\r\n"
is requesting the url /SendSMS/index from the site
so it is basically requesting the following
http://www.example.com/SensSMS/index

So put the code to send the sms in the SendSMS controller (or change the code I have given you to the correct Controller and Model in the url)

#14
[eluser]xwero[/eluser]
Cron is an application on your server that takes care of repetitive tasks. If you have a a management interface from your hoster like cpanel you will see it as an option. The only thing you need to do is writing a script for something you want to do and direct the cron to that url and set up an interval.

#15
[eluser]Negligence[/eluser]
You might be able to do something like this in your Main index function, but play around with the code, I haven't tested it.

Code:
public function index()
{
   include('path/to/otherController.php');
   $controllerObj = new ControllerName();

   call_user_func(array($controllerObj, 'index'));
}

#16
[eluser]cinewbie81[/eluser]
tonanbarbarian :-

Here's what i do:
Code:
class sms {

  function background() {
    $fp = fsockopen("http://smssystem.localproject", 80, $errno, $errstr, 30);
    if (!$fp) {
      return;
    }
    $request = "GET /sendsms/index HTTP/1.1\r\n"
      ."Host: http://smsproject.localproject/index.php\r\n"
      ."Connection: Close\r\n\r\n";
    fwrite($fp, $request);
    fclose($fp);
  } // background()
} /
I call from this function from controller, but it doesnt execute the test2/index sendsms/index statement Sad .. i dont know why ..

xwero - I still struggling to write a cron script to execute the function in the controller ..

Negligence - your function might work, but it doesnt execute the statement background isn't it ?

#17
[eluser]cinewbie81[/eluser]
Ok I have the following two files:
a) sms.php (controller file),
b) background.php (not a controller file, and it's in controller though)

a) sms.php
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Sms extends Controller {
    
    function Sms()
    {    
        parent::Controller();
        $this->load->helper(array('url','form'));
        $this->load->library(array('table','pagination'));
        $this->load->model('dbmodel', 'sql', TRUE);
        $this->load->library('validation');
        $this->load->database();
    }


    function SendSMS()
    {    
        // Code to send sms here
    }

    function backgroundprocess()
    {
        exec("nohup php /var/www/myproject/system/application/controllers/background.php > /dev/null &");                
    }
}
?>



b) background.php
Code:
<?php
   include('/var/www/myproject/system/application/controllers/sms.php');
   $controller = new Sms();
   call_user_func(array($controller, 'SendSMS'));
?>

When i browse www.mysite.com/index.php/sms/sendsms, it will send the sms successfully, but of course it's not background process.
So i try to create another function in my Sms controller call backgroundprocess() as shown above. Now when i browse www.mysite.com/index.php/sms/backgroundprocess , it didnt send me any sms ... any idea why ?

#18
[eluser]Ciaro[/eluser]
[quote author="tonanbarbarian" date="1197028875"]try something like this (totally untested)

you use fsockopen to make a connection to the url, writing the data of the request, then just close the socket without reading the reply

Code:
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
  log_message('error', 'Unable to connect to server');
  return;
}
$request = "GET /SendSMS/index HTTP/1.1\r\n"
  ."Host: www.example.com\r\n"
  ."Connection: Close\r\n\r\n";
fwrite($fp, $request);
// normally now we would get the result like so
//$result = '';
//while (!feof($fp)) {
//  $result .= fgets($fp, 128);
//}

fclose($fp);
[/quote]

Great solution. Really helped me alot. Thanks for sharing!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.