KBK
KBK

Reputation: 375

CI function not working from external php script

I need to run a CI controller > method from external php script.. that external php script sends some values using curl

$url = 'http://domain.com/ci_system/billing/success';

$fields = array(
    'ammount' => $extra[0]['ammount'],
    'email' => $extra[0]['email'],
    'cemail' => $extra[0]['cemail'],
    'rcode' => $completeResponse->getResponseCode(),
    'message' => $completeResponse->getResponseText()
);
$post = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec($ch);
curl_close($ch);

above success function I have to send a email. curl sends all required data to success function but is is not working. when i manually trigger this function it works..

controller > method

$this->cart->destroy();
$ord = array(
    'online' => 1
);
$this->Billing_model->updateOrder($ord, $this->session->userdata('data_o_i'));
//send email to the client
$this->load->library('email');
$config['protocol'] = 'sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);

$this->email->from('[email protected]', 'Dsd Retail New Order');
$this->email->to($this->input->post('[email protected]'));

$this->email->subject('You have a new message for yor order');
$this->email->message($this->input->post('email'));

Upvotes: 4

Views: 199

Answers (1)

Pradeep Sanjaya
Pradeep Sanjaya

Reputation: 1846

First of all check if your action is working fine and give you expected behaviour if you call it from url.

I have following setup with ci3.

Hosts

$ cat /etc/hosts
127.0.0.1 ci3.local

Apache sites-available

cat /etc/apache2/sites-available/ci3.conf

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName ci3.local
    DocumentRoot /home/user/project/ci3

    ErrorLog ${APACHE_LOG_DIR}/ci3_error.log
        CustomLog ${APACHE_LOG_DIR}/ci3_access.log combined

    SetEnv APPLICATION_ENV "development"

    <Directory /home/user/project/ci3>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        Allow from all
    </Directory> 

</VirtualHost>

ci3/.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

ci3/application/config/routes.php

$route['billing/success'] = 'billing/success';

ci3/application/controllers/Billing.php

<?php
class Billing extends CI_Controller {

    public function success()
    {
        log_message('debug', 'Billing.success');
        log_message('debug', $this->input->post('amount'));
    }
}

Now if I request http://ci3.local/billing/success from browser, it will log following output to my log file

DEBUG - 2015-12-07 08:47:34 --> UTF-8 Support Enabled
DEBUG - 2015-12-07 08:47:34 --> Global POST, GET and COOKIE data sanitized
DEBUG - 2015-12-07 08:47:34 --> Billing.success
DEBUG - 2015-12-07 08:47:34 --> 
DEBUG - 2015-12-07 08:47:34 --> Total execution time: 0.0019

How do I call it from terminal and check if it is working?

$ curl --data "amount=200" http://ci3.local/billing/success

Then I should see my log entries like below

DEBUG - 2015-12-07 08:49:38 --> UTF-8 Support Enabled
DEBUG - 2015-12-07 08:49:38 --> Global POST, GET and COOKIE data sanitized
DEBUG - 2015-12-07 08:49:38 --> Billing.success
DEBUG - 2015-12-07 08:49:38 --> 200
DEBUG - 2015-12-07 08:49:38 --> Total execution time: 0.0021

How do I call this action from a CURL over http?

ci3/curl.php

<?php
$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => "http://ci3.local/billing/success",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => array(
        'amount' => '300'
    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);

Call curl.php from terminal

php5 curl.php

Then I should see my log entries like below

DEBUG - 2015-12-07 08:53:41 --> UTF-8 Support Enabled
DEBUG - 2015-12-07 08:53:41 --> Global POST, GET and COOKIE data sanitized
DEBUG - 2015-12-07 08:53:41 --> Billing.success
DEBUG - 2015-12-07 08:53:41 --> 300
DEBUG - 2015-12-07 08:53:41 --> Total execution time: 0.0021

Upvotes: 2

Related Questions