-2

我正在使用SmartFile来存储用户提交的文件。要在网站上显示图像文件,我可以链接到它们,但如果文件丢失,我无法选择显示默认图像。或者防止热链接。或设置浏览器缓存等。

他们的 API 被限制为每分钟 180 个请求。因此,在繁忙的网站上显示图像并不好。

我曾尝试使用get_headers来检查预期的文件是否存在,然后file_get_contents,如果确实如此,但这非常缓慢且效率低下。仅使用file_get_contentsout get_headersfirst 非常慢!

我在这里最好的选择是什么?我还没试过用curl。我想我可以只用一个请求就可以得到标题和文件,但是file_get_contents速度很慢,我想 curl 不会更快。

4

2 回答 2

0

尝试从另一个站点抓取图像,是的,它使用 file_get_content

<?php 

if(!isset($_GET['read'])){
 $url = "http://mangaku.web.id/baca-komik-naruto-terbaru-bahasa-indonesia/"; 
 $input = @file_get_contents($url) or die("Could not access file: $url");
 $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; 
 if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
 foreach($matches as $match) {
     if (strpos($match[2],'chapter')!== false OR strpos($match[3],'naruto-chapter')!== false) {
    echo "<a href='http://localhost/mangaku.php?read=".$match[2]."'>".$match[3]."</a><br>";
}
 } 
 }
}

 if(isset($_GET['read'])){
 $url = $_GET['read']; 
 $input = @file_get_contents($url) or die("Could not access file: $url");
 $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; 
 if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
 foreach($matches as $match) {
     if (strpos($match[2],'bp.blogspot.com') !== false) {
  echo "<center><image src='".$match[2]."'></center><br>";
  //echo $match[2]."<br>";
}
 } 
 }
}
?>
于 2014-05-26T05:14:38.733 回答
0

您可以通过 JavaScript 加载和检查浏览器。因此,您的应用程序只需将链接传递给文件,用户将看到加载图像,而 JavaScript 将检查图像是否存在。

使用 jQuery 的示例:

<img src="loading.gif">
<script>
    $.ajax({
        type: 'HEAD',
        url: 'http://example.com/image.jpg',
        success: function() {
            $('img').attr('src','http://example.com/image.jpg');
        },
        error: function() {

        }
    });
</script>

如果您不需要隐藏图像路径,则不必使用file_get_contents加载图像。

您还可以有一些数据库表,您将在其中缓存图像地址。并每小时检查链接是否仍在工作或损坏。如果它被破坏,您将在数据库中将该图像的 url 更改为missing.jpg

您也可以考虑从smartfile 转移到 DropBox 或其他服务。

于 2014-05-25T23:17:16.060 回答