file_get_contents($url) Error returned

Asked

Viewed 122 times

0

Hello, I found a function here in the stack for a problem similar to mine but I’m having a problem.

The function is:

function file_get_contents_retry($url, $attemptsRemaining=3) {
    $content = file_get_contents($url);
    $attemptsRemaining--;

    if( empty($content) && $attemptsRemaining > 0 ) {
        return file_get_contents_retry($url, $attemptsRemaining);
    }

    return $content;
}

My problem is that after I get the information I have a echo "sucesso"; which returns to a $.post function.

However, it seems to me that when the function of some error in some attempt, the $.post, in addition to catching the success of echo it catches this error.

I need to know how to remove this error.

Any hint?

  • This $url is a path (local file) or is a URL (http) even?

  • @W.Faustino is an http URL itself.

  • Some Servers block fopen or file_get_contents, I think the best solution is to use PHP Curl http://www.php.net/curl

  • @W.Faustino the site does not block. The problem is that sometimes it trouble to open, and others it opens correctly. The function it tries 3x. As an example, let’s say that the first attempt fails and the second one works. When I get the feedback I wish was just the "success" it returns beyond the success the error of the first attempt.

  • What version of PHP vc are you using? If PHP 7 tries to use a Try catch.

  • @W.Faustino php version is 5.6.31. Try catch only works with PHP 7?

Show 1 more comment

1 answer

0

Replaces the first line of the function by:

$content=@file_get_contents($url);

Stay like this:

function file_get_contents_retry($url, $attemptsRemaining=3) {
    $content = @file_get_contents($url);
    $attemptsRemaining--;

    if( empty($content) && $attemptsRemaining > 0 ) {
        return file_get_contents_retry($url, $attemptsRemaining);
    }

    return $content;
}

Browser other questions tagged

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