Check if a link is active or broken

Asked

Viewed 3,134 times

2

I wonder how I can use PHP to check if the link of download of servers such as Mega, Google, Filehero and others are active if they are not presenting the result of offline link.

In this case URL this offline, how can I know if a certain link is off? programmatically.

I found several codes to validate the existence of pages on the internet, with everything not valid links server.

I found this code on the internet, but it doesn’t work properly because even if the link is online the code puts the result as a broken link how can I fix it in this code ? Or someone could pass me one that works for any mega download link, 4shared, Minhateca etc...

<?php 

$url = 'http://mega.co.nz/#!0Q9zGIwb!v_CAoVPESQ9TExR7H66kA_ZPjjaZCZtBUHZE5_OmcIc'; 
$result = @file_get_contents($url); 

// verifica se a url existe 
if ($result !== false): 
// procura pelo id do formulário catcha id='captchaform' 
$pos = stripos($result, 'captchaform'); 

// se encontrar o id='captchaform' então é a página dos downloads 
if ($pos !== false): 
echo 'Url On'; 
endif; 

else: 
echo 'Url off!'; 
endif; 

?>
  • Have you read about the API of Mega?

  • http://julien-marchand.fr/blog/using-the-mega-api-with-php-examples/

  • more ai only check in case the mega need to check any link.

  • 1

    Then this question can solve your problem, http://answall.com/questions/1819/howto checkout se-uma-imagem-existe-num-url-remote

  • send me a valid url

  • As I’m a bit busy at the moment, I’ll stop by, try to take a look here https://github.com/codeguy/modern-php/tree/master/04-components/url-scanner-component that should help you, later, if possible, I’ll try to give you a better answer with this! In short, it is an example of the book, which checks whether the url is valid or not!

  • I gave an update on the question.

  • @Rodrigo That code I posted only checks if the answer code indicates if the request was successful, I tested here only with Mega, and it seemed to work. However, if you intend to check links of a wider range of Servers, use http://urlchecker.org/ there is even an API.

  • Okay thanks for the help.

Show 4 more comments

2 answers

2


One way to do this is to perform a request and check if the response code is 200, indicating that the request was successful.

See an example using library cURL:

/*
  Argumentos:
    $url:    A URL a ser verificada
    $limite: Define o tempo limite. É opcional, o padrão é 25s
  Retorno:
    true:  Se a URL estiver disponível
    false: Se a URL estiver quebrada
*/
function verificarLink($url, $limite = 25){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);        // Inicia uma nova sessão do cURL
    curl_setopt($curl, CURLOPT_TIMEOUT, $limite); // Define um tempo limite da requisição
    curl_setopt($curl, CURLOPT_NOBODY, true);     // Define que iremos realizar uma requisição "HEAD"
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); // Não exibir a saída no navegador
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Não verificar o certificado do site

    curl_exec($curl);  // Executa a sessão do cURL
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200; // Se a resposta for OK, a URL está ativa
    curl_close($curl); // Fecha a sessão do cURL

    return $status;
}

Example of use:

$link = "http://mega.co.nz/#!0Q9zGIwb!v_CAoVPESQ9TExR7H66kA_ZPjjaZCZtBUHZE5_OmcIc";
$status = verificarLink($link);
if ($status) {
    echo "O link fornecido está disponível!";
} else {
    echo "O link fornecido está quebrado.";
}

0

If you look at the URL, it ends with #! and a lot of strange characters: the #! indicates that, in reality, who redirects the user to the right place is a piece of Javascript that runs in the user’s browser. You could run the Javascript of the page using something like the phpjs, but the most sensible solution is to try to convert these Urls using the _escaped_fragment_ google (in case, you would try to access http://mega.co.nz/?_escaped_fragment_=0Q9zGIwb!v_CAoVPESQ9TExR7H66kA_ZPjjaZCZtBUHZE5_OmcIc, but it’s not clear to me if the mega.co.nz follows the specification of Google).

Browser other questions tagged

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