我有一個運行貨幣轉換的簡單檢查的PHP文件。當我通過瀏覽器運行它時,它完美地工作,但我的目標是構建一個cron。當我通過SSH運行腳本:PHP Cron Job json_encode錯誤
php /path/to/file.php
我得到如下:
PHP Warning: json_encode(): Invalid UTF-8 sequence in argument in /path/to/file.php on line 36
其中第36行是:
fwrite($fh, json_encode($conversions));
...其中$轉換是一個簡單的單二維陣列
這裏是檔案:
$conversions = array();
$currencies = json_decode(file_get_contents("/path/to/currencies.json"), true);
foreach($currencies as $cur=>$data){
//make string to be put in API
$string = "1USD=?".$data['code'];
//Call Google API
$google_url = "http://www.google.com/ig/calculator?hl=en&q=".$string;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $google_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$result = curl_exec($ch);
//Explode result to convert into an array
$result = explode('"', $result);
$converted_amount = explode(' ', $result[3]);
$conversion = $converted_amount[0];
//$conversion = $conversion * $amount;
$conversions[$cur] = $conversion;
if($conversion==0){ exit('0 Return Error'); }
curl_close($ch);
}
$fh = fopen("/path/to/currency_conversions.json", 'w') or die("can't open file");
fwrite($fh, json_encode($conversions));
fclose($fh);
那麼什麼是$轉換數組?一些文字?隨機二進制垃圾? – 2012-07-25 15:02:31
這是一系列國家代碼(關鍵),它們的轉換率(val)如下:USD => 1,CAD => 1.23,EUR => 1.05 ... – Fluidbyte 2012-07-25 15:03:40
我們需要了解正在填充的轉換費用。它是從命令行參數填充的嗎?從數據庫?你的文件系統和數據庫是UTF-8編碼還是拉丁文? – 2012-07-25 15:05:44