summaryrefslogtreecommitdiff
path: root/iplayer.php
blob: 5234fd4e505646e7d8c4aefae1dc93b4cc8c8fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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 iPlayer URL:<br>
e.g. http://www.bbc.co.uk/iplayer/episode/b04tr8c4/<br>
<input name="url" id="url" size="40" type="text">
<input type="submit"><br>
You can also use ?id= to specify a programme ID directly to bypass this form<br>
e.g. http://iplayer.of.je/?id=b04tr8c4</p>
</form>
<?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($curl, CURLOPT_INTERFACE, CURLIFACE);
curl_setopt($curl, CURLOPT_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($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$notjson = curl_exec($curl);
curl_close($curl);

// Extract the actual JSON inside the JavaScript
$jsonpos = strpos($notjson, '{');
$json = json_decode(substr($notjson, $jsonpos, strlen($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();
  }
}
?>