简体   繁体   中英

Bilingual CodeIgniter cron job

I built a bilingual application with CodeIgniter and now I need 2 cron jobs to send emails out to certain users.

The problem I'm having is that I'm unable to run the cron jobs and I believe it is because of the libraries I have to run CodeIgniter in many languages.

I'm attempting to run this command: php /home/[username]/public_html/projects/[project name]/index.php cron remind

The email I'm getting after it runs is the following:

Content-type: text/html

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  array_key_exists() [<a href='function.array-key-exists'>function.array-key-exists</a>]: The first argument should be either a string or an integer</p>
<p>Filename: core/MY_Lang.php</p>
<p>Line Number: 153</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  array_key_exists() [<a href='function.array-key-exists'>function.array-key-exists</a>]: The first argument should be either a string or an integer</p>
<p>Filename: core/MY_Lang.php</p>
<p>Line Number: 153</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - headers already sent by (output started at /home/vchris/CI-2.0.1/core/Exceptions.php:170)</p>
<p>Filename: core/MY_Lang.php</p>
<p>Line Number: 73</p>

</div>

MY_Lang.php header is on line 73

if ((!$url_ok) && (!$this->is_special($uri_segment['parts'][0]))) // special URI -> no redirect
        {
            // set default language
            $CFG->set_item('language', $this->languages[$this->default_lang()]);

            $uri = (!empty($this->uri)) ? $this->uri: $this->default_uri;
            $uri = ($uri[0] != '/') ? '/'.$uri : $uri;
            $new_url = $CFG->config['base_url'].$this->default_lang().$uri;

            header("Location: " . $new_url, TRUE, 302);
            exit;
        }

MY_Lang.php return is on line 153

// default language: first element of $this->languages
 function default_lang()
    {
        $browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
        $browser_lang = substr($browser_lang, 0,2);
        return (array_key_exists($browser_lang, $this->languages)) ? $browser_lang: 'en';
    }

Basically when you browse my site if you don't have a language set in the url, it will automatically add /en/. Is there a way to bypass that or to set it in the controller? I think MY_Lang is called before the controller. So maybe there's a way to make it so I don't have to use index.php to enter CodeIgniter.

It looks to me like $browser_lang is empty on line 153 because $_SERVER['HTTTP_ACCEPT_LANGUAGE'] is empty on line 151 and the ternary is setting the variable to to an empty string. Now when it checks for the key in $this->languages , it's throwing your errors.

I'm guessing this is related to calling it from the command line rather than a browser and that $_SERVER variable is not being set. I'd do a check on line 153 to see if $browser_lang is empty like this:

// default language: first element of $this->languages
function default_lang()
{
    $browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
    $browser_lang = substr($browser_lang, 0,2);
    return (!empty($browser_lang) && array_key_exists($browser_lang, $this->languages)) ? $browser_lang : 'en';
}

The other option might be to have your cron job use wget instead of calling PHP directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM