【发布时间】:2016-02-16 15:36:57
【问题描述】:
我正在搜索一个用 php 编写的带有用户输入的货币转换器,因此用户可以输入一个值然后输出它。这是主脚本。
<?php
$from_currency = 'EUR';
$to_currency = 'USD';
$amount = 1;
$results = converCurrency($from_currency,$to_currency,$amount);
$regularExpression = '#\<span class=bld\>(.+?)\<\/span\>#s';
preg_match($regularExpression, $results, $finalData);
echo $finalData[0];
function converCurrency($from,$to,$amount){
$url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to";
$request = curl_init();
$timeOut = 0;
curl_setopt ($request, CURLOPT_URL, $url);
curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
$response = curl_exec($request);
curl_close($request);
return $response;
}
?>
我必须在这个脚本中添加什么用户可以输入他自己的值。
【问题讨论】:
-
使用
$_GET或$_POST。另外,请确保验证您的输入。 -
互联网上似乎有不少 api 提供货币转换并以比手动解析 Google 的结果更有用的格式返回数据
标签: php html converter currency