Created
September 14, 2014 04:19
-
-
Save angelbladex/22a5b3a8d7ac6e07ee9b to your computer and use it in GitHub Desktop.
Prueba de concepto para obtener fechas de operativos Mercal de la página de Mercal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//original code written by @willicab > https://pastebin.mozilla.org/6456892 | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
$mes = array( | |
"ENERO" => "01", | |
"FEBRERO" => "02", | |
"MARZO" => "03", | |
"ABRIL" => "04", | |
"MAYO" => "05", | |
"JUNIO" => "06", | |
"JULIO" => "07", | |
"AGOSTO" => "08", | |
"SEPTIEMBRE" => "09", | |
"OCTUBRE" => "10", | |
"NOVIEMBRE" => "11", | |
"DICIEMBRE" => "12", | |
); | |
$salida = array(); | |
$ch = curl_init(); | |
curl_setopt($ch,CURLOPT_URL, "http://www.mercal.gob.ve/"); | |
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE); | |
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30); | |
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); | |
curl_setopt($ch,CURLOPT_REFERER,'http://www.google.co.ve/'); | |
curl_setopt($ch,CURLOPT_TIMEOUT,30); | |
$html = curl_exec($ch); | |
preg_match_all("/<a\s+[^>]*\bhref\s*\=\s*[\x27\x22](?<Url>[^\x27\x22]*)[\x27\x22]\s*title\s*\=\s*[\x27\x22](OPERATIVOS DIARIOS *)(?<Fecha>[^\x27\x22]*)[\x27\x22]>([1-9]|1[0-9]|2[0-9]|3[01])<\/a>/i", $html, $array, PREG_PATTERN_ORDER); | |
for($i=0; $i<count($array['Url']); $i++) { | |
$arrayFecha = explode(" ", $array['Fecha'][$i]); | |
$fecha = date("Y") . "-" . $mes[$arrayFecha[3]] . "-" . $arrayFecha[1]; | |
$palabras = explode(" ", $array['Fecha'][$i]); | |
$fechaHumana = ''; | |
foreach($palabras as $elemento) { | |
$fechaHumana .= ucfirst(strtolower($elemento))." "; | |
} | |
$salida[$fecha] = array("url" => "http://www.mercal.gob.ve/" . $array['Url'][$i], "fecha" => $fechaHumana); | |
} | |
print json_encode($salida); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment