optimizar tiempos de file-get-contents en php
Abril 5th, 2010 by David Rodriguez
A veces tenemos que cargar xml o contenido externo a nuestra web.
En php, tenemos la función file-get-contents para leer un fichero y devolver un string. En ese caso, podemos tener diferencia de tiempos de carga entre ejecutar un xml en el navegador .. y cargar ese xml en un string.
Esto puede ser debido a las politicas de seguridad de los servidores donde cargamos la información.
Para reducir estos tiempos y que nuestra web no se ralentice, es mejor cambiar esa función por esta otra que nos definimos nosotros a través del método curl.
function get_url_contents($url){
$crl = curl_init();
$timeout = 25;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
Tenemos mejoras en tiempos de un 75% menores en la carga.
Posts relacionados
This entry was posted on Lunes, Abril 5th, 2010 at 1:02 pm and is filed under Internet, Programacion. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Abril 6th, 2010 at 9:29 am
[...] Aviso Legal « optimizar tiempos de file-get-contents en php [...]