Producing Participatory Media
Week 7 - February 27

Topics:

  • PHP 101
  • Blip.TV API in PHP
  • Peer to Peer Distribution


  • PHP 101

    Basic HTML

    <html> <!-- Required for all HTML pages -->
    	<head> 
    		<title>The Top Bar Title</title>
    	</head>
    	<body>
    		Some information
    		<!-- A Comment Tag -->
    	</body>
    </html> <!-- Closing tag, required -->
    		
    Example 1

    Basic HTML with PHP
    PHP = Hypertext Preprocessor

    <html> <!-- Required for all HTML pages -->
            <head>
                    <title>The Top Bar Title</title>
            </head>
            <body>
                    Some information
    		<br> <!-- A line break -->
                    <!-- A Comment Tag -->
    		<?  // Denotes the start of PHP processing
            		echo "The Date and Time is: ";  // print a string, end with a semicolon
            		$mydata = "July 29, 2004"; // Assign a variable 
            		echo $mydata; // print the string variable
    		?> 
    
            </body>
    </html> <!-- Closing tag, required -->
    		
    Example 2
    Take note of the different comment styles.
    The "//" style within PHP and "<--" outside of PHP (normal HTML)
    Variables begin with "$".
    "=" are assignment.
    "echo" is the print function.
    ";" ends line of code.
    PHP files are saved with ".php" not ".html"

    PHP Functions
    Can occur anywhere in the page within "<? ?>" tags

    <?
    function printDate()
    {
    	echo "The Date and Time is: ";
    	$mydata = "Feb. 27, 2007";
            echo $mydata;
    }
    ?>
    <html>
            <head> 
                    <title>The Top Bar Title</title>
            </head>
            <body>
                    <? 
    			printDate();
                    ?>
            </body>
    </html>
    
    Example 3

    Built-in PHP Functions
    More than you can imagine: Everything from writing XML to creating images on the fly including creation of Flash files and running Java applications
    PHP.net is your best friend
    <?
    function printDate()
    {
            echo "The Date and Time is: ";
            $mydata = date("g:i:s A T D. M, j Y");
    	/*  -- Multiline comment
    	"date" is a built-in function that has some interesting formatting capabilities.
    	*/
            echo $mydata;
    }
    ?>
    <html>
            <head>
                    <title>The Top Bar Title</title>
            </head>
            <body>
                    <?
                            printDate();
                    ?>
            </body>
    </html>
    
    Example 4
    date page from the PHP Manual

    PHP with Forms
    PHP is ideal for generating HTML forms and even more so for dealing with data that comes back from forms.

    A Basic HTML Form
    <html>
            <head>
                    <title>A Basic HTML Form</title>
            </head>
            <body>
    		<form action="form_example.php" type="get">
    			First Name:<input type="text" name="first_name"><br>
    			Last Name:
    			<select name="last_name">
    				<option value="Smith">Smith</option>
    				<option value="Doe">Doe</option>
    			</select>
    			<br>
    			<input type="submit" value="Submit Me">
    		</form>
            </body>
    </html>
                    
    HTML Form Example

    Form Processing with PHP Dealing with data submitted via a form
    <?
            /* 
            Description: Helper function to get values from a form post (type="post" or query string (type="get")
            Returns: value of key or null on failure
            */
            function getPOSTorGETValue($key)
            {
                if (isset($_POST[$key]))
                {
                    $value = $_POST[$key];
                }
                else if (isset($_GET[$key]))
                {
                    $value = $_GET[$key];
                }
                else
                {           
                    $value = null;
                }    
                return $value;
            }
    ?>
    <html>
            <head>
                    <title>The Form Output</title>
            </head>
            <body>
                    <?
    			$first_name = getPOSTorGETValue("first_name");
    			$last_name = getPOSTorGETValue("last_name");	
    			echo "Your First Name is: " . $first_name . "<br>";
    			echo "Your Last Name is: " . $last_name . "<br>";
                    ?>
            </body>
    </html>
    
    Example Form Output


    Blip.TV API

    Blip.TV has a very nice platform for uploading and hosting video and it is free (for most uses). They allow you to cross post your video, they automatically transcode to Flash, they provide direct links to the files so you can do what you like with them (unlike YouTube) and they have a revenue sharing arrangement for ads served on the files.

    One other thing they have is a REST (Representational State Transfer) API which is accessed through normal HTTP connections using XML. You can learn quite a lot about the API and the capabilities of it on Blip's Wiki

    Thankfully, some enterprising individuals have come up with a simplified means to use portions of the API through PHP. The phpBlip package provides a nicer interface to developing applications that pull data from Blip, much the way the phpYoutube package allows for pulling data from YouTube.

    The easiest way to get started with the phpBlip package is to unpack it, upload it to your webserver and look at the example.php output.

    If you view the source of the example.php file you will see that there are two calls at the beginning to start using it:
    include("phpBlip.php");
    $phpBlip = new phpBlip();	
    	
    The first call includes the phpBlip class definition and related files. The second actually creates an object out of the class. This object is what can be used to query the blip site.

    There are two functions that can be used with the phpBlip object:

    find_posts which searches blips database for posts that match your parameters.
    Function definition:
    function find_posts($userid = null, $search = null, $explicit = false, $language_code = "en", $category = null, $license = null, $has_media = 1)
    	

    Example:
    $results = $phpBlip->find_posts();
    for ($i = 0; $i < sizeof($results); $i++)
    {
    	print_r($results[$i]);
    }
    	

    Of course, you can add in whatever parameters you would like:
    $results = $phpBlip->find_posts(null,"cats");
    for ($i = 0; $i < sizeof($results); $i++)
    {
    	print_r($results[$i]);
    }
    	

    The other function is:
    find_post which allows you to pull more information about an individual post (such as a link to file):
    print_r($phpBlip->find_post($some_post_id));	
    	

    Putting the two together allows you to do a bunch of different things for example:
    $vids = $phpBlip->find_posts(null, "test");	
    for ($i = 0; $i < sizeof($vids); $i++)
    {
    	$current_video = $phpBlip->find_post($vids[$i]["id"]);
    
    	for ($m = 0; $m < sizeof($current_video['media']); $m++)
    	{
    		if ($current_video['media'][$m]['link_type'] == 'video/x-flv')
    		{
    ?>	
    			<a href="http://itp.nyu.edu/~sve204/ppm_spring07/player.php?m=<?php echo $current_video['media'][$m]['link_href'] ?>"><? echo $current_video['title'] ?></a>					
    			<br><br>	
    <?
    		}
    	}
    }	
    	

    Example output

    Peer to Peer Distribution (BitTorrent)

    Distributing large files across the internet has always been a challenge. While the internet itself is decentralized most content on the internet is served from a single location to many clients. In a sense, using the internet in this manner is akin to broadcasting. Unfortunately, as is not the case with broadcasting the more people that "consume" any particular piece of content drive up the cost of hosting that content. Bandwidth costs money.

    Peer to Peer file sharing networks were created in part to alleviate this problem. Unfortunately most of the peer to peer file sharing networks still contained remnants of the broadcast scenario. For instance, Napster ran a service that didn't host the content but had a central service to track where content is available on the network. Several of the Napster clones while getting better at true distributed content distribution still had some remnants of the original Napster model.

    Enter BitTorrent. Bram Cohen, developed BitTorrent as a way around these problems. He released it open source and it has quickly become the de-facto means for peer to peer delivery of media content on the internet.



    More Information:
  • The Official BitTorrent Home Page
  • Wired 13.01: The BitTorrent Effect
  • Azureus: Java BitTorrent Client
  • Broadcast Machine


  • Some BitTorrent Services/Trackers:

  • DV Guide
  • Prodigem
  • Legal Torrents
  • etree.org tracker


  • Some (perhaps less legal) torrents:

  • Torrentspy
  • isoHunt
  • TorrentBox.com
  • Mininova


  • Recently many of the BitTorrent/TV trackers have been shutdown by the MPAA (Supernova, et al..). This doesn't spell the end of BitTorrent by any means (see Hollywood Wants BitTorrent Dead). Because BitTorrent is not centralized and anyone can run a tracker and the fact that it is open source means that it will continue being used for both legitimate and illegitimate ways for a long time to come. I believe that the MPAA cracking down means more opportunity for independent producers and tracker operators for greater exposure, especially as the technology becomes easier to use and integrated with more and more applications.

    Recent Legal Actions:

  • Torrentfreak >> Torrent sites under attack
  • MPAA Press Release
  • Hollywood Wants BitTorrent Dead


  • BitTorrent + RSS = Broadcatching

    Enter RSS.. Of course, using RSS with torrent enclosures you can distribute your media ala podcasting and videoblogging. This reduces the strain on your bandwidth and creates a more equal playing field for all.

    Using the same means for including audio and video enclosures in RSS feeds for Movable Type/WordPress we can add BitTorrent enclosures.
    See: MT-Enclosures (for MT users). and: Mime-Types.

    <enclosure url="http://dv.open4all.info/bblog/torrent_files/flea.mov.torrent" length="2422" type="application/x-bittorrent"/>

    Broadcatching Applications

    Applications that read RSS feeds and automatically download torrent enclosures.

  • RSS Import Plugin for Azureus
  • WritTorrent Various BitTorrent utlities
  • Buttress - Bittorrent RSS application
  • TVMistress
  • Videora


  • Video Blogging Aggregators that support torrents

  • FireAnt
  • Democracy
  • I/On


  • More Information:

  • Broadcatching with BitTorrent
  • engadget - How-To: BroadCatching using RSS + BitTorrent to automatically download TV shows