开发者

curl, fopen & file_get_contents Randomly throw "The connection was reset" error in browser

I have tried the 3, file_get_contents, curl & fopenbut the problem is I get the "The connection was reset" error randomly, i mean if it works for url A but fails with url B.

Any help would be appreciated.

Heres the code:

function readRemoteFile($url, $use = FOP) {
$url = urldecode($url);
switch ($use) {
    case FOP:

        $handle = fopen($url, 'r');
        while (!feof($handle)) {
            $content.=fread($handle, 2048);
        }
        return $content;
        break;
    case FGC :

        if (!$handle = file_get_contents($url)) { 
            return FALSE;
        } else {
            while ($chunk = fread($handle, 2048)) {
                $content .= $chunk;
            }
            fclose($handle);
            return $content;
        }
        break;
    case CURL :
        $ch = curl_init(urldecode($url));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        ob_start ();
        $c = curl_exec($ch);
    开发者_如何学JAVA    curl_close($ch);
        ob_end_clean ();
        return trim($c);
        break;
}

}

regards,


This:

case FGC :

    if (!$handle = file_get_contents($url)) { 
        return FALSE;
    } else {
        while ($chunk = fread($handle, 2048)) {
            $content .= $chunk;
        }
        fclose($handle);
        return $content;
    }

Is wrong. It should be:

case FGC :
  return file_get_contents($url);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜