itms_preview.php
<html><title>iTunes Music Video Preview</title><head></head><body>
<?php
$server = 'ax.phobos.apple.com.edgesuite.net';
if (isset($_POST['url']))
{
$url = $_POST['url'];
preg_match('/id=(\d*)/', $url, $video_id);
if (count($video_id) < 2)
$error = "No id was found in the URL.";
else
$video_id = $video_id[1];
}
else
{
$error = 'Right click the video, "Copy iTunes Music Store URL" '
.'then paste above.';
$url = '';
}
?>
<div align="center">
<h3>iTunes Music Video Preview</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 Video URL:
<input type="text" name="url" id="url" size=35 maxlength=200
value="<?=$url?>">
<input type="submit" id="submit" name="submit" value="submit">
</form><hr>
<?php
if (isset($error)) error($error);
if (!$fp = fsockopen($server, 80)) error("Couldn't connect to server.");
$headers = "GET /WebObjects/MZStore.woa/wa/viewVideo?id={$video_id}".
"&prvw=1 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:.*\.m4v)"/', $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>