function http_read() { // args: $url = func_get_arg(0); $post_data = func_num_args() == 2 ? func_get_arg(1) : null; // headers: $headers = array("User-Agent: Mozilla/Curl", "Expect: "); // post: if ($post_data != null) { $headers[] = "Content-Type: application/x-www-form-urlencoded; charset=utf-8"; $headers[] = "Content-Length: " . mb_strlen($post_data, "UTF-8"); } // cookies: if (session_id() == "") { session_start(); } if (!array_key_exists("__ps_http_read_cookie", $_SESSION)) { $_SESSION["__ps_http_read_cookie"] = array(); } if (count($_SESSION["__ps_http_read_cookie"])) { $array = array_map(create_function('$k, $v', 'return "$k=$v";'), array_keys($_SESSION["__ps_http_read_cookie"]), array_values($_SESSION["__ps_http_read_cookie"])); $headers[] = "Cookie: " . implode("; ", $array); } // server comunication: global $_http_read_curl_; if (!isset($_http_read_curl_)) { $_http_read_curl_ = curl_init(); } curl_setopt($_http_read_curl_, CURLOPT_URL, $url); curl_setopt($_http_read_curl_, CURLOPT_HTTPHEADER, $headers); curl_setopt($_http_read_curl_, CURLOPT_HEADER, true); if ($post_data != null) { curl_setopt($_http_read_curl_, CURLOPT_POST, true); curl_setopt($_http_read_curl_, CURLOPT_HTTPGET, false); curl_setopt($_http_read_curl_, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($_http_read_curl_, CURLOPT_POSTFIELDS, $post_data); } else { curl_setopt($_http_read_curl_, CURLOPT_POST, false); curl_setopt($_http_read_curl_, CURLOPT_HTTPGET, true); curl_setopt($_http_read_curl_, CURLOPT_CUSTOMREQUEST, "GET"); } curl_setopt($_http_read_curl_, CURLOPT_RETURNTRANSFER, true); curl_setopt($_http_read_curl_, CURLOPT_TIMEOUT, 600); curl_setopt($_http_read_curl_, CURLOPT_CONNECTTIMEOUT, 4); curl_setopt($_http_read_curl_, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($_http_read_curl_, CURLOPT_FOLLOWLOCATION, false); curl_setopt($_http_read_curl_, CURLOPT_AUTOREFERER, true); $result = curl_exec($_http_read_curl_); // head + body split: list($head, $body) = explode("\r\n\r\n", $result, 2); // cookies: if (preg_match_all('#Set-Cookie: ([^=]*)=([^;]*);.*#', $head, $matches)) { $_SESSION["__ps_http_read_cookie"] = array_merge($_SESSION["__ps_http_read_cookie"], array_combine($matches[1], $matches[2])); } // fim: se resposta foi um "Location", retorna página apontada. Se não, converte para utf8 e retorna body if (preg_match('#Location: (.*)?#', $head, $matches)) { return http_read($matches[1]); } // encoding: if (preg_match('#Content-Type: .*charset=(.*);?#', $head, $matches) and $matches[1] != "utf8") { $body = mb_convert_encoding($body, "utf8"); } return $body; }
function hack_movimentacao_vendas($data_inicial, $data_final) { $url = "https://services.redecard.com.br"; $url .= "/novoportal/portals/servicoSharepoint/extratoonline/IS_ExtratosRedecard_Extrato.aspx"; $url .= "?BankLine=&txtnu_pdv=&box_sel=010&data_inicial={$data_inicial}&data_final={$data_final}&moeda=R&Selpvs=0&Box=010&flgTrava=0"; $html = http_read($url, ""); // extrai info do html $ok = preg_match('#table class="frm_INS"([^>]*)>(.*?)</table>#s', $html, $matches); // somente a tabela if (!$ok) { return array(); } $html = preg_replace("#(<b>|</b>|<a.*?>|</a>|<font.*?>|</font>)#s", "", $matches[2]); // retira formatação $dados = extract_data($html); $dados = array_map(create_function('$i', 'return $i["td"];'), $dados["tr"]); // header / resumo $header = array_shift($dados); $resumo = array(); for ($i = 0; $i <= 2; $i++) { $resumo_ = array_pop($dados); $nome = array_pop(explode(" ", $resumo_[0])); $resumo[$nome]["VALOR_LIQUIDO"] = str_replace(",", ".", str_replace(".", "", array_pop($resumo_))) + 0; $resumo[$nome]["DESCONTO_TAXAS"] = str_replace(",", ".", str_replace(".", "", array_pop($resumo_))) + 0; $resumo[$nome]["VALOR_CORRECOES"] = str_replace(",", ".", str_replace(".", "", array_pop($resumo_))) + 0; $resumo[$nome]["VALOR_VENDAS"] = str_replace(",", ".", str_replace(".", "", array_pop($resumo_))) + 0; } // normaliza data (p/ iso), números foreach ($dados as $k => $v) { $dados[$k][0] = \DateTime::createFromFormat("d/m/Y", $v[0])->format("Ymd"); $dados[$k][1] = \DateTime::createFromFormat("d/m/Y", $v[1])->format("Ymd"); $dados[$k][7] = str_replace(",", ".", str_replace(".", "", $v[7])) + 0; $dados[$k][8] = str_replace(",", ".", str_replace(".", "", $v[8])) + 0; $dados[$k][9] = str_replace(",", ".", str_replace(".", "", $v[9])) + 0; $dados[$k][10] = str_replace(",", ".", str_replace(".", "", $v[10])) + 0; } // fim return array("resumo" => $resumo, "cabecalho" => $header, "dados" => $dados); }