itms_trailer.php
<html><title>iTunes Movie Trailer</title><head></head><body>
<?php
$server = 'movies.apple.com';
foreach (array('small', 'medium', 'large') as $size)
{
$size = isset($_POST[$size]) ? $size : '';
if (!empty($size)) break;
}
if (!empty($size))
{
$url = $_POST['url'];
if (!preg_match('/index.xml$/', $url))
$error = "The URL should end with index.xml";
else
$url_size = str_replace('index.xml', "{$size}.xml", $url);
}
else
{
$error = 'Right click the trailer (not size), '
.'"Copy iTunes Music Store URL" then paste above.';
$url = '';
}
?>
<div align="center">
<h3>iTunes Movie Trailer</h3>
<font size="2">
See the script and get a copy
<a href="http://www.s-seven.net/itunes.html">here</a>.<br><br>
<i>Tested with iTunes 6.0</i>
</font><br><br>
<form id="form" name="form" action="<?=$_SERVER['PHP_SELF']?>" method="post">
iTunes Trailer URL: <input type="text" name="url" id="url" size=35
maxlength=200 value="<?=$url?>">
<input type="submit" id="small" name="small" value="small">
<input type="submit" id="medium" name="medium" value="medium">
<input type="submit" id="large" name="large" value="large">
</form><hr>
<?php
if (isset($error)) error($error);
if (!$fp = fsockopen($server, 80)) error("Couldn't connect to server.");
$headers = "GET {$url_size} HTTP/1.1\r\n";
$headers .= "Host:{$server}\r\n";
$headers .= "User-Agent: iTunes/6.0 (Macintosh; U; PPC)\r\n";
$headers .= "Accept-Language: en-us, en;q=0.50\r\n";
$headers .= "Accept-Encoding: gzip\r\n";
$headers .= "Connection: close\r\n\r\n";
if (!fwrite($fp, $headers)) error("Error sending request.");
$payload = "";
while (!feof($fp)) $payload .= fread($fp, 128);
fclose($fp);
preg_match("/[Cc]ontent-[Ll]ength: (\d+)/", $payload, $length);
$length = (count($length) < 2 ? -1 : $length[1]);
preg_match("/[Cc]ontent-[Ee]ncoding: (.+)\r\n/", $payload, $encoding);
$encoding = (count($encoding) < 2 ? '' : $encoding[1]);
$payload = substr($payload, strpos($payload, "\r\n\r\n"), strlen($payload));
$payload = ltrim($payload, "\r\n");
if ($length > -1 && strlen($payload) != $length)
{
error("Payload is incorrect length.");
}
if (preg_match("/gzip/", $encoding))
{
$payload = gzinflate(substr($payload,10));
}
preg_match('/<MovieView.*url="(http:.*\.mov)"/', $payload, $movie);
if (count($movie) < 2)
{
echo "Got something unexptected:<br></div>";
echo "<pre>", htmlentities($payload), "</pre><div>";
}
else echo "<a href=\"{$movie[1]}\">{$movie[1]}</a>";
function error($error)
{
echo $error;
echo "</div></body></html>";
exit;
}
?>
</div></body></html>