php美元汇率接口

/**
     * 
     * @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 "";
        
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值