我正在使用一个外部网络服务,该服务将返回一个图像URL,我将在我的网站上显示该图像。例如:
$url = get_from_web_service();
echo '<img url="'.$url.'" />';
除了要显示100张图片之外,一切正常,但是调用网络服务变得费时并消耗资源。
//the problem
foreach($items as $item) {
$url = get_from_web_service($item);
echo '<img url="'.$url.'" />';
}
现在我在考虑两种选项:
//Option1: Using php get_file_contents():
foreach($items as $item)
{
echo '<img url="url_to_my_website/get_image.php?id='.$item->id.'" />'
}
get_image.php :
$url = get_from_web_service($id);
header("Content-Type: image/png");
echo file_get_contents($url);
//Option2: Using ajax:
echo '<img scr="dummy_image_or_website_logo" data-id="123" />';
//ajax call to the web service to get the id=123 and get the url then add the src attribute to that image.
想法
- 第一种选择似乎更为直接,但我的服务器可能会超载并参与每个图像请求。
- 第二种选择完全由浏览器和Web服务完成,因此我的服务器根本不涉及。但是对于每个图像,我都要进行两次调用,即一个ajax调用以获取图像URL,另一个调用以获取图像。因此,加载时间可能会有所不同,并且大量调用可能会导致ajax调用失败。
信息
- 该页面将显示约50张图片。
- 该服务将同时被约100个用户使用。
- 我无法控制Web服务,因此无法更改其功能,并且它不接受每个调用的多个图像ID。
我的问题
- 是否有更好的选择?
- 如果没有,我应该遵循哪个选项?最重要的是为什么我应该遵循那个选项?
谢谢