/**
*
* @param type $url
* @param type $type
* @param type $arr
* @return type
*/
public function http_curl($url, $type = 'get', $arr = '') {
if($arr){
$o = "";
foreach ( $arr as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$arr = substr($o,0,-1);
}
$ch = curl_init();
$user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
curl_setopt($ch, CURLOPT_USERAGENT,$user_agent);
curl_setopt($ch, CURLOPT_URL, $url); //设置访问的地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取的信息返回
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($type == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
}
$output = curl_exec($ch); //采集
if (curl_error($ch)) {
return curl_error($ch);
}
return $output;
}
/**
* 美元-人民币汇率
*/
public function usd_to_cny() {
$output = $this->ex_rate1();
if($output){
return $output;die;
}
$output = $this->ex_rate2();
if($output){
return $output;die;
}
$output = $this->ex_rate3();
if($output){
return $output;die;
}
}
/**
* 汇率接口1
*/
public function ex_rate1() {
$from = "USD";
$to = "CNY";
$money = 1;
$url = "https://api.jijinhao.com/plus/convert.htm?from_tkc=".$from."&to_tkc=".$to."&amount=".$money;
$output = controller("Common")->http_curl($url);
if($output){
$ex_rate = substr( $output , strpos($output,"'")+1 , -1 );
}
return $ex_rate;
}
/**
* 汇率接口2
*/
public function ex_rate2() {
header("Content-type:text/html;charset=utf-8");
$url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=1%E7%BE%8E%E5%85%83%E7%AD%89%E4%BA%8E%E5%A4%9A%E5%B0%91%E4%BA%BA%E6%B0%91%E5%B8%81&resource_id=6017&t=1517817481143&format=json";
$output = controller("Common")->http_curl($url);
if($output){
$match = '';
preg_match('/\"number2\"\:\"(.*)\"\,\"content1Mini\"/',$output,$match);
if(is_numeric($match[1])){
$rate = $match[1];
return $rate;
}
}
return "";
}
/**
* 汇率接口3
*/
public function ex_rate3() {
header("Content-type:text/html;charset=utf-8");
$url = "https://www.currencydo.com/index/api/hljs/hbd/USD_CNY.json?t=0.38758558136894394";
$output = controller("Common")->http_curl($url);
if($output){
$arr = explode("#", $output);
if($arr[4]){
return $arr[4];
}
}
return "";
}
/**
* 汇率接口4
*/
public function ex_rate4() {
header("Content-type:text/html;charset=utf-8");
$url = "https://api.exchangerate-api.com/v4/latest/USD";
$output = controller("Common")->http_curl($url);
if($output){
$output = json_decode($output,true);
return $output['rates']['CNY'];
}
return "";
}