fetch_description.php (1224B)
1 <?php
2
3 /* Build URL to fetch the description from */
4 $url = "https://www.twitch.tv/" . $_GET["stream"];
5
6 /* Set useragent via URL context: Lie about who we are!
7 * Twitch.tv does not return the proper description to browsers anymore
8 * However, it does return the proper description to Google's crawler...
9 */
10 $opts = array(
11 "http" => array(
12 "method" => "GET",
13 "header" => array(
14 "User-Agent: " . "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
15 )
16 )
17 );
18 $context = stream_context_create($opts);
19
20 /* Fetch HTML meta data (up until body starts) */
21 $handle = fopen($url, "r", false, $context);
22 if ($handle) {
23 $html = stream_get_line($handle, 0, "<body>");
24 fclose($handle);
25 } else {
26 $html = "";
27 }
28
29 /* Fetch description line */
30 $desc_raw = preg_replace(
31 '/^.*(<meta[^>]+og:description[^>]+>).*/',
32 '\1',
33 $html
34 );
35
36 /* Ensure that double quotes are used consistently */
37 $desc_raw = preg_replace(
38 '/\'/',
39 '"',
40 $desc_raw
41 );
42
43 /* Extract description string */
44 $desc = preg_replace(
45 '/^.*content="([^"]+)".*$/',
46 '\1',
47 $desc_raw
48 );
49
50 /* return the html decoded description line */
51 echo htmlspecialchars_decode($desc);
52
53 ?>