title: PHP: Displaying an image from a web service
body: <p>I'm using an external web service that will return an image URL which i will display in my website, For example :</p>
  
  <pre><code>$url = get_from_web_service();
  echo '&lt;img url="'.$url.'" /&gt;';
  </code></pre>
  
  <p>everything is working fine except if i have 100 images to show then calling the web service become time &amp; resources consuming.</p>
  
  <pre><code>//the problem
  foreach($items as $item) {
     $url = get_from_web_service($item);
     echo '&lt;img url="'.$url.'" /&gt;';
  }
  </code></pre>
  
  <p>So now i'm considering two options:</p>
  
  <pre><code>//Option1: Using php get_file_contents():
  foreach($items as $item)
  {
     echo '&lt;img url="url_to_my_website/get_image.php?id='.$item-&gt;id.'" /&gt;'
  }
  
  get_image.php :
  
  $url = get_from_web_service($id);
  header("Content-Type: image/png");
  echo file_get_contents($url);
  
  
  //Option2: Using ajax:
  
  echo '&lt;img scr="dummy_image_or_website_logo" data-id="123" /&gt;';
  
  //ajax call to the web service to get the id=123 and get the url then add the src attribute to that image.
  </code></pre>
  
  <p><strong>THOUGHTS</strong></p>
  
  <ol>
  <li>First option seems more straight forward, but my server might be
  overloaded and involved in every single image request.</li>
  <li>Second option it's all done by browser &amp; web service so my server is not involved at all. but for each image i'm making 2 calls 1 ajax call to get the image URL and another one one to get the image. so loading time might be vary and ajax calls might fail for large number of calls.</li>
  </ol>
  
  <p><strong>Information</strong></p>
  
  <ol>
  <li>Around 50 Images will be displayed in that page.</li>
  <li>This service will be used by around a 100 user at a given time.</li>
  <li>I have no control over the web service so i can't change its functionality and it doesn't accept more than 1 image ID for each call.</li>
  </ol>
  
  <p><strong>My Questions</strong></p>
  
  <ol>
  <li>Any better option i should consider?</li>
  <li>If not, which option should I follow? and most important why i should follow that one?</li>
  </ol>
  
  <p>Thanks</p>