This code example uses the PEAR HTTP request lib.
It demonstrates how you can transfer a video, encode it in Flash format and get it back to your FTP in only one HTTP request.
<?php
require_once "HTTP/Request.php";
$video_url = "http://myhost.com/video.avi";
// We want to encode in Flash format
$format_id = "31";
// The encoded video will be uploaded to this FTP location
$ftp = "ftp://login:passwd@myhost.com/videos";
// You'll be notified when all the process is done
$ping_after_encode = "http://myhost.com/ping/success.php";
// You'll be notified if there is an error during the process.
$ping_if_error = "http://myhost.com/ping/error.php";
// Custom field to track your user, this is just an example
$my_site_user_id = $session->getCurrentUser->id;
// The video you'll show on your site
// Note that the class Video is not defined, it's just an example
// to interact with the database
$video = new Video(array("user_id" => $my_site_user_id, "status" => "unavailable"));
$video->save();
$my_site_video_id = $video->id;
$req =& new HTTP_Request("http://heywatch.com/download.xml");
// Authenticate via your HeyWatch login / password
$req->setBasicAuth("johndoe", "foo");
// It's a POST request
$req->setMethod(HTTP_REQUEST_METHOD_POST);
// Add all the needed parameters
$req->addPostData("url", $video_url);
$req->addPostData("format_id", $format_id);
// Force encoding after the download is complete
$req->addPostData("automatic_encode", "true");
$req->addPostData("ftp_directive", $ftp);
$req->addPostData("ping_url_after_encode", $ping_after_encode);
$req->addPostData("ping_url_if_error", $ping_if_error);
// Some custom fields to track your own objects
// You'll receive them in the ping after encode / ping if error
$req->addPostData("my_site_user_id", $my_site_user_id);
$req->addPostData("my_site_video_id", $my_site_video_id);
// Send the request
if (!PEAR::isError($req->sendRequest())) {
echo $req->getResponseBody(); // XML response
}
?>
When all the process is done, you’ll receive the HTTP notification at:
/ping/success.php
<?php $filename = $_POST["filename"]; // Custom fields are available in $_GET array $my_site_user_id = $_GET["my_site_user_id"]; $video_id = $_GET["my_site_video_id"]; // Map the FTP location and the HTTP server // ftp://myhost.com/videos => http: //myhost.com/videos $video_location = "http://myhost.com/videos/".$filename; $thumbnail_location = $video_location.".jpg"; // Update the video to be available on your site $video = Video::find($video_id); $video->setStatus = "available"; $video->setVideoLocation = $video_location; $video->setThumbnailLocation = $thumbnail_location; $video->save(); ?>
If the process failed, you’ll receive the notification at:
/ping/error.php
<?php // Custom fields are available in $_GET array $my_site_user_id = $_GET["my_site_user_id"]; $video_id = $_GET["my_site_video_id"]; // Update your video to be flagged as error $video = Video::find($video_id); $video->setStatus = "error"; $video->save(); // Send a mail to the user about the error ?>