Consulta cotação dolar via PHP

<?php

include '../model/Conexao.php';
include 'phpQuery-onefile.php';

$conexao = new Conexao();

function simple_curl($url, $post = array(), $get = array()) {
$url = explode('?', $url, 2);
if (count($url) === 2) {
$temp_get = array();
parse_str($url[1], $temp_get);
$get = array_merge($get, $temp_get);
}

$ch = curl_init($url[0] . "?" . http_build_query($get));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
// curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($ch);
}

$html = AbreSite('https://ptax.bcb.gov.br/ptax_internet/consultarUltimaCotacaoDolar.do');

$html = array_filter(explode(' ', strip_tags($html)), 'ehBranca' );
foreach ($html as $key => $dolar) {
$conexao->comando('update configuracao set vldolar = '. str_replace(',', '.', $dolar).' where codconfiguracao = 1');
}

function soNumero($str) {
return preg_replace("/[^0-9]/", "", $str);
}

function ehBranca($str) {
if(!isset($str) || $str == NULL || trim($str) == '' || strstr($str, ',') == FALSE){
return false;
}elseif(strstr($str, ',') != FALSE){
$separador_valor = explode(',', $str);
if(count($separador_valor) == 2 && strlen(trim($separador_valor[1])) > 1){
return true;
}else{
return false;
}

}
}

function AbreSite($url) {
$site_url = $url;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $site_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
ob_start();
curl_exec($ch);
curl_close($ch);
$file_contents = ob_get_contents();
ob_end_clean();
return $file_contents;
}

Comentários

Postagens mais visitadas deste blog

Instalação NetBeans

Calcular frete pelos correios via PHP