<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">
<title>BBC iPlayer video stream extractor</title>

</head>

<body>

<?php
define
("CURLIFACE""192.168.122.9");

// Grab the programme ID with GET, or with POST, or present a URL form
if (isset($_GET['id'])) {
  
$id $_GET['id'];
} else if (isset(
$_POST['url'])) {
  
$idpos strpos($_POST['url'], "/episode/") + 9;
  
$idlen strpos($_POST['url'], "/"$idpos) - $idpos;
  if (
$idlen 1) {
    
$idlen strlen($_POST['url']) - $idpos;
  }
  
$id substr($_POST['url'], $idpos$idlen);
} else {
?>
<form action="./" method="post">
<p>Enter a non-live BBC iPlayer URL:<br>
e.g. http://www.bbc.co.uk/iplayer/episode/b06h9y66/<br>
<input name="url" id="url" size="40" type="text">
<input type="submit"><br>
<br>
You can also use ?id= to specify a programme ID directly to bypass this form<br>
e.g. <a href="http://iplayer.of.je/?id=b06h9y66">http://iplayer.of.je/?id=b06h9y66</a><br>
<br>
You can open the resulting file in <a href="https://www.videolan.org/">VLC</a>, or your preferred player.<br>
Alternatively, use the direct URL directly in your player.<br>
<br>
If you get a blank page, please try again as the request occasionally fails.</p>
</form>

<script type="text/javascript">
  document.getElementById("url").focus();
  document.getElementById("url").select();
</script>

<p><small><a href="./iplayer.phps">Source code?</a></small></p>

</body>

</html>
<?php
  
exit();
}

// Grab iPlayer HTML page
$html file_get_contents("http://www.bbc.co.uk/iplayer/episode/$id/");

// Extract the VPID from the page
$vpidpos strpos($html'vpid":"') + 7;
$vpidlen strpos($html'"'$vpidpos) - $vpidpos;
$vpid substr($html$vpidpos$vpidlen);

// Grab the JSON which appears to be within some JavaScript
$curl curl_init();
curl_setopt($curlCURLOPT_INTERFACECURLIFACE);
curl_setopt($curlCURLOPT_URL"http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/vpid/$vpid/format/json/mediaset/apple-ipad-hls/jsfunc/JS_callbacks39");
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
curl_setopt($curlCURLOPT_HEADERfalse);
$notjson curl_exec($curl);
curl_close($curl);

// Extract the actual JSON inside the JavaScript
$jsonpos strpos($notjson'{');
$json json_decode(substr($notjson$jsonposstrlen($notjson) - $jsonpos 3), true);

// Find an akamai_hls_open stream and HTTP redirect to it
foreach ($json['media']['0']['connection'] as $connection) {
  if (
$connection['supplier'] == "akamai_hls_open") {
    
header("Location: " $connection['href']);
    exit();
  }
}
?>