Doubt with PHP file_get_contents

0

My code running on the pc works 100% but when I play on the server it does not capture the information. follow code for you to help me

<link type="text/css" rel="stylesheet" href="http://jsuol.com.br/c/_template/v1/_geral/css/styles.css?&file=geral&type=parse&versao=v1&plataforma=web&portal=uol&media=webpage&cache=19t3e89gn" />
<link type="text/css" rel="stylesheet" href="http://jsuol.com.br/c/_template/v1/_geral/css/styles.css?&file=especifico&type=parse&versao=v1&plataforma=web&portal=uol&estacao=economia&estacao-id=economia&cache=19t3e89gn" />
<link type="text/css" rel="stylesheet" href="http://jsuol.com.br/c/_template/v1/web/uol/css/internas/economia/cotacoes/styles-cotacoes.css?&cache=19t3e89gn" />
<link type="text/css" rel="stylesheet" href="http://jsuol.com.br/c/economia/cotacoes/cotacoes.css?v13&&cache=19t3e89gn" />
<link type="text/css" rel="stylesheet" href="http://jsuol.com.br/c/_template/v1/web/uol/css/estrutura/conteudo-auxiliar.css?&cache=19t3e89gn" />
<?php

$url = file_get_contents('http://economia.uol.com.br/cotacoes/');
preg_match_all('/<section class="barra-ticker pg-bgcolor3">(.+)<\/p> <\/li> <\/ul> <\/div> <\/section>/s', $url, $conteudo);
$exibir = $conteudo[0][0];
$retirar = array('');
$exibir = str_replace($retirar, '', $exibir);
echo $exibir;
                ?>
    
asked by anonymous 25.08.2015 / 14:40

1 answer

1

Go to the file php.ini and change the following lines;

From: allow_url_fopen = Off

To: allow_url_fopen = On

If you do not have access to php.ini you can also use;

ini_get('allow_url_open') or ini_set('allow_url_fopen', 1)

If it does not work ...

You can also use CURL to do this, right after the opening of the PHP tag on your page in question, put the code below:

function my_file_get_contents( $site_url ){
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $site_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
ob_start();
curl_exec($ch);
curl_close($ch);
$file_contents = ob_get_contents();
ob_end_clean();
return $file_contents;
}

Now instead of using file_get_contents('http://.....') you can use my_file_get_contents('http://.....')

I found in some forums the following sentence:

  

The problem will be solved, using the file_get_contents function is a serious security flaw, and it is recommended to create the above cURL function.

    
25.08.2015 / 14:48