pedrotuga Posted November 28, 2006 at 05:43 AM Report #67261 Posted November 28, 2006 at 05:43 AM Ok... o serviço de hosting que uso desactivou a abertura de ficheiros remotos por razões de segurança. ainda não percebi quais. Estas pequenas funções podem ser útieis sempre que o allow_url_fopen estiver a off. Se tiverem duvidas verifiquem no phpinfo(). O problema pode ser contornado usando a biblioteca curl. aqui vão as alternativas. Encontrei este código no wiki da dreamhost e embrulhei-o em duas funções. <?php function alternative_file_get_contents($url){ $ch = curl_init(); $timeout = 0; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, "$url"); curl_setopt ($ch,CURLOPT_USERAGENT, "Mozilla"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); return $file_contents; } function alternative_file($url){ $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); $lines = array(); $lines = explode("\n", $file_contents); return $lines; } ?>
-IceBurn- Posted January 20, 2007 at 06:55 AM Report #77395 Posted January 20, 2007 at 06:55 AM Para não receberem um erro 302 (Movido) em certos sites (Google por exemplo) acrescentem pelo menos esta linha à função: curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); Para uma maior fiabilidade, desenvolvi um pouco mais estas funções, aqui fica o resultado: <?php function alternative_file_get_contents($url){ $header[] = $_SERVER["HTTP_ACCEPT"]; $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, "$url"); curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt ($ch, CURLOPT_HTTPHEADER, $header); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); return $file_contents; } function alternative_file($url){ $header[] = $_SERVER["HTTP_ACCEPT"]; $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, "$url"); curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt ($ch, CURLOPT_HTTPHEADER, $header); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); $lines = array(); $lines = explode("\n", $file_contents); return $lines; } ?>
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now