Jump to content

[PHP] Alternativas curl para quem tem o allow_url_fopen a off


Recommended Posts

Posted

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;
}
?>
  • 1 month later...
Posted

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;
}

?>

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.