CI function does not work with external PHP script

I need to run the CI controller method from an external php script .. which 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 need to send an email. curlsends all the necessary data to the success function, but does not work. when I manually run 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('noreply@zxzxzxz.com', 'Dsd Retail New Order');
$this->email->to($this->input->post('omail@gmail.com'));

$this->email->subject('You have a new message for yor order');
$this->email->message($this->input->post('email'));
+4
source share
1 answer

First of all, check if your action is working fine and give the expected behavior if you call it from the URL.

I have the following setup with ci3.

Hosts

$ cat /etc/hosts
127.0.0.1 ci3.local

Available Apache Sites

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

<VirtualHost *:80>
        ServerAdmin email@email.com
        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'));
    }
}

, http://ci3.local/billing/success ,

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

, ?

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

,

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

CURL 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);

curl.php

php5 curl.php

,

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
+2

Source: https://habr.com/ru/post/1618843/


All Articles