我有一个 PHP 文件,它正在运行一个简单的货币转换检查。当我通过浏览器运行它时它可以完美运行,但我的目标是构建一个 cron。当我通过 SSH 运行脚本时:
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 是一个简单的一维数组
这是文件:
$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);