雅虎的接口:
http://download.finance.yahoo.com/d/quotes.csv?e=.txt&f=sl1d1t1&s={from}{to}=x
- 参数e:返回的格式默认为csv,这里写成了txt
- 参数f:返回的内容包含哪些功能,默认是汇率值、日期、时间
- 参数s:{from}和{to}分别代表左右两边两个货币单位名称,比如:USD,EUR,GBP等。注意后面的=x不能少
代码中的send_get方法为发送一个get请求,参见PHP发送一个简单的Get请求
public static function get_curreny($from,$to){ $url = "http://download.finance.yahoo.com/d/quotes.csv?e=.txt&f=sl1d1t1&s={from}{to}=x"; $url = self::content_replace( ['from'=>$from,'to'=>$to],$url); $data = self::send_get($url); if($data){ $data = explode(',', $data); $currency['value'] = $data[1]; $currency['time'] = $data[2]; } return $currency; } public static function content_replace($data, $template) { foreach ($data as $key => $value) { $pattern[] = '/\{' . $key . '\}/'; $replacement[] = $value; } return preg_replace($pattern, $replacement, $template); }