You are here

Soft proxying resources from other domains for Shockwave

If you try to access resources from domains other than the domain a Shockwave movie runs on, you get an ugly and intimidating dialog box. If there are no other options than to use the files off the external domain (eg if you are using a web service, like I am for the SPi-V Flickr interface), you can proxy the contents using php and curl (if your webserver is configured to allow this).

Here's what I use to get and serve an image from a Flickr server and maskerade it as a local file:

function getImage($imageurl) {
 // create a new curl resource
 $curlinst = curl_init();

 // set URL and other appropriate options
 curl_setopt($curlinst, CURLOPT_URL, $imageurl);
 curl_setopt($curlinst, CURLOPT_FOLLOWLOCATION, true);
 curl_setopt($curlinst, CURLOPT_HEADER, true);
 curl_setopt($curlinst, CURLOPT_NOBODY, true);
 curl_setopt($curlinst, CURLOPT_RETURNTRANSFER, true);

 // grab URL and pass it to the browser
 $ret = curl_exec($curlinst);

 if (empty($ret)) {
  // some kind of an error happened
  curl_close($curlinst);
  exit(curl_error($curlinst));
 } else {
  $info = curl_getinfo($curlinst);

  if (empty($info['http_code'])) {
  curl_close($curlinst);
  exit("No HTTP code was returned");
  } else if($info['http_code']>=400) {
   // set any error to a 404 for now
   curl_close($curlinst);
   header("HTTP/1.0 404 Not Found");
   exit();
  }
 }

 curl_close($curlinst);

 header("Content-Type: ".$info['content_type']);
 header("Content-Length: ".floor($info['download_content_length']+1));

 // create a new curl resource
 $curlinst = curl_init();

 // set URL and other appropriate options
 curl_setopt($curlinst, CURLOPT_URL, $imageurl);
 curl_setopt($curlinst, CURLOPT_FOLLOWLOCATION, true);
 curl_setopt($curlinst, CURLOPT_HEADER, false);
 curl_setopt($curlinst, CURLOPT_NOBODY, false);
 curl_setopt($curlinst, CURLOPT_RETURNTRANSFER, false);

 curl_exec($curlinst);

 // close curl resource, and free up system resources
 curl_close($curlinst);
}

Note that this construction is quite bandwidth inefficitent, as your webserver will now have double the bandwidth load: once for getting the resource from the external host, and once for sending it to the shockwave movie. But it works, and you don't get the dialog box.

Topics: