【发布时间】:2013-03-06 16:02:15
【问题描述】:
我有一个从终端运行的 php 脚本,它的作用是:
- 从数据库中抓取一行数据(表中存储了这个脚本专门处理的JSON字符串);
- 将 JSON 字符串转换为数组并准备要插入数据库的数据。
- 将所需数据插入数据库
这是脚本:
#!/usr/bin/php
<?PHP
//script used to parse tweets we have gathered from the twitter streaming API
mb_internal_encoding("UTF-8");
date_default_timezone_set('UTC');
require './config/config.php';
require './libs/db.class.php';
require './libs/tweetReadWrite.class.php';
require './libs/tweetHandle.class.php';
require './libs/tweetPrepare.class.php';
require './libs/pushOver.class.php';
require './libs/getLocationDetails.class.php';
//instatiate our classes
$twitdb = new db(Config::getConfig("twitterDbConnStr"),Config::getConfig("twitterDbUser"),Config::getConfig("twitterDbPass"));
$pushOvr = new PushOver(); // push error messages to my phone
$tweetPR = new TweetPrepare(); // prepares tweet data
$geoData = new getLocationDetails($pushOvr); // reverse geolocation using google maps API
$tweetIO = new TweetReadWrite($twitdb,$tweetPR,$pushOvr,$geoData); // read and write tweet data to the database
/* grab cached json row from the ORCALE Database
*
* the reason the JSON string is brought back in multiple parts is because
* PDO doesnt handle CLOB's very well and most of the time the JSON string
* is larger than 4000 chars - its a hack but it works
*
* the following sql specifies a test row to work with which has characters like €$£ etc..
*/
$sql = "
SELECT a.tjc_id
, dbms_lob.substr(tweet_json, 4000,1) part1
, dbms_lob.substr(tweet_json, 8000,4001) part2
, dbms_lob.substr(tweet_json, 12000,8001) part3
FROM twtr_json_cache a
WHERE a.tjc_id = 8368
";
$sth = $twitdb->prepare($sql);
$sth->execute();
$data = $sth->fetchAll();
//join JSON string back together
$jsonRaw = $data[0]['PART1'].$data[0]['PART2'].$data[0]['PART3'];
//shouldnt needs to do this, doesnt affect the outcome anyway
$jsonRaw = mb_convert_encoding($jsonRaw, "UTF-8");
//convert JSON object to an array
$data = json_decode($jsonRaw,true);
//prepares the data (grabs the data I need from the JSON object and does some
//validation etc then finally submits to the database
$result = $tweetIO->saveTweet($data); // returns BOOL
echo $result;
?>
现在,如果我使用./proc_json_cache.php 或php proc_json_chache.php 从终端运行它,它工作正常,数据以UTF-8 编码到达数据库并且一切正常,数据库中的数据看起来像这样£$@€ < test。
如果我通过 CRON 调用此脚本,它仍会保存数据,但像 €£ 等特殊字符只是正方形,数据库中的数据看起来像这样 ��$@��� < test。
到目前为止,我尝试将以下几行添加到我的 crontab 中:
TERM=xterm
SHELL=/bin/bash
这是为了匹配我当前的 shell ENV 会话设置,并将以下内容添加到调用我的 php 脚本的 bash 脚本中:
export NLS_LANG="ENGLISH_UNITED KINGDOM.AL32UTF8"
export LANG="en_GB.UTF-8"
再次匹配我当前的 shell ENV 设置,但是当脚本在终端中从 cron 与直接运行时,我仍然遇到字符编码问题。
有没有其他人遇到过类似的问题,可以说明如何解决这个问题? 提前致谢。
编辑:
这里是有关服务器的更多信息:
操作系统:SUSE Linux Enterprise Server 11
PHP:5.2.14
【问题讨论】:
-
尝试在您的控制台上输入
env并检查是否应该在您的脚本中导出其他一些env值。 -
我应该寻找哪些其他值?
-
用 UTF-8 代替 UTF-8,你能试试 ISO-8859-1 看看它是否有效。
-
它必须是 UTF-8,因为那是 oracle 数据库设置使用的内容,twitter API 也使用 UTF-8 完成所有操作,总体而言,它更通用以保持所有内容 UTF-8 .
-
您可以尝试将交互式 shell 环境中的 everything 添加到脚本中,如果可行,请开始删除不需要的变量。
标签: php oracle bash utf-8 crontab