Check for remote file, slow, PHP

Asked

Viewed 105 times

0

I’m having trouble checking with remote files, the structure is as follows:

function endereco_existe($url) {  
$h = get_headers($url);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', $h[0] , $status);
$result = $status[1];
if($result == "200")
{
    return true;//verdadeiro
}else 
{
    return false;//falso
}
}

this function works perfectly ,however what happens when I call this function often makes the page very slow and even does not open , I did the test taking this function and the problem does not appear,

this function is used in this context:

function lista($ini,$fin,$url,$ext,$op=0,$stp=1)
{
        echo "<p id='rep'></p>";

        echo "<script>function rep(url,i){var t = '<style>div.img{position: relative; width: 100%;}div.img > img{position: absolute; right: 3%; margin-right: 0px; top: 4%; margin-top: 0px; background-color: ; width:12%;opacity: 0.2; filter: alpha(opacity=20);}</style><div style=text-align:center; id=top>Lista '+i+'</div><div style=text-align:right;><a href=javascript:void(0); onclick=fechar();>[Fechar]</a></div><div class=img><video width=100% controls><source src='+url+' type=video/mp4>Seu Navegador não Suporta Repoduzir esse video baixe o Firefox ou Google Chrome</video><img src=logo.png></div>';document.getElementById('rep').innerHTML = t;} function fechar(){document.getElementById('rep').innerHTML =' ';}</script>";
        for($i=$ini;$i<=$fin;$i++)
        {
        if($i<10)
        {
        $i = "0".$i;
        }
        if($stp==1)
        {
            if(endereco_existe($url.$i.$ext)==false)
            { break;}
        }

        echo "Lista 1".($i-$op)."&nbsp";
        echo "<a href='#top' onclick='rep(`".$url.$i.$ext."`,".($i-$op).")'>Repoduzir</a>";
        echo " | ";
        echo "<a download='Lista ".$i.".mp4' href='".$url.$i.$ext."' target='_blank'>Baixar</a>";
        echo "<br />";
        }
}

It lists some videos, passed to the function that are listed by the suffix 01,02 and so on that are determined in "for" (www.examplo.com/video01.mp4), I wonder if there is any solution in this check if the file exists and in the listing of the same?

  • 1

    you make a request with Curl on get_headers()?

  • So I used the Curl already and continued slow to check. function curl_info($url){&#xA; $ch = curl_init();&#xA; curl_setopt( $ch, CURLOPT_URL, $url );&#xA; curl_setopt( $ch, CURLOPT_HEADER, 1);&#xA; curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );&#xA; curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 0 );&#xA; curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );&#xA; $content = curl_exec( $ch );&#xA; $info = curl_getinfo( $ch );&#xA; return $info;&#xA; }

  • 2

    You are not checking the existence of files, you are checking Urls, each different url access takes some time, imagine multiple checks of different web pages.

1 answer

0

What you are doing is not checking files but checking Urls, each URL checked is a connection and a different "download", varying according to content and size this can take a lot of time, since you will have to read content by content:

<?php
function endereco_existe($url) {
      $ch = curl_init($value);

      curl_setopt($ch, CURLOPT_NOBODY, true); //não traz o corpo

      //Desabilitei a checagem SSL, mas é preferivel configurar no php.ini
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //não retorna a transferencia

      curl_exec($ch);

      return curl_getinfo($ch[$key], CURLINFO_HTTP_CODE) == 200;
}

But keep in mind that this doesn’t get much better, just helps a little, you can still try sub-connections, like this (as an example http://php.net/manual/en/function.curl-multi-exec.php#119506):

// Todas url gravadas em array
$url = array();
$url[] = 'http://www.link1.com.br';
$url[] = 'https://www.link2.com.br';
$url[] = 'https://www.link3.com.br';

// Setando opção padrão para todas url e adicionando a fila para processamento
$mh = curl_multi_init();
foreach($url as $key => $value){
  $ch[$key] = curl_init($value);
  curl_setopt($ch[$key], CURLOPT_NOBODY, true);
  curl_setopt($ch[$key], CURLOPT_HEADER, true);
  curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch[$key], CURLOPT_SSL_VERIFYHOST, false);

  curl_multi_add_handle($mh,$ch[$key]);
}

// Executando consulta
do {
  curl_multi_exec($mh, $running);
  curl_multi_select($mh);
} while ($running > 0);

// Obtendo dados de todas as consultas e retirando da fila
foreach(array_keys($ch) as $key) {
  $url = curl_getinfo($ch[$key], CURLINFO_EFFECTIVE_URL);

  if (curl_getinfo($ch[$key], CURLINFO_HTTP_CODE) == 200) {
      echo $url, ' está disponivel';
  } else {
      echo $url, ' está inacessível';
  }

  curl_multi_remove_handle($mh, $ch[$key]);
}

// Finalizando
curl_multi_close($mh);
  • Thanks for the tip I will try, do this way, remembering that the files that I check if they exist, are institutional videos on average 800mb to 1gb. , is there another way to display this or store the links, suggests something ?

  • @Thiagoci would be interesting a daily cache, I will see if I formulate something and put here :)

  • Grateful, ;-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.