API Examples - Web Service

Secure Upload Example:

Your API key, secret, and gatekeeper should be kept from your users when building a web application. If you wish to offer users the ability to upload videos (seemingly to your own site), then you can do so using single-use tokens. By using these tokens, you can prevent your security credentials from being revealed through web forms. For more information on how single-use tokens work, click here.

Generally - an upload implementation would involve two actions on your end. The first, is an upload form that is properly formatted with authentication values. The second, is a callback function to handle the upload response, involving the returned hash value for the media sent to the API. This example is written in PHP and should be easily portable to any other web language.

Upload Form:


<?

$api_key = "FsUsername";
$secret = "FsSecret";
$gatekeeper = "FsGatekeeper";
$callback = "http://www.yoursite.com/fs_token_callback.php";
$sig = md5($gatekeeper."generatetoken");

// Get a single use token:
$params = array();
$params['key'] = $api_key;
$params['secret'] = $secret;
$params['sig'] = $sig;

$token_request_string = send_post_request('http://www.framesocket.com/api/auth/generatetoken.php',$params);
$token_request = json_decode($token_request_string);

if( $token_request->success != "1" ) {
	
	die("Could not fetch token.");
	
}

$token = $token_request->token;

?>

<form action="http://www.framesocket.com/api/media/upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="key" value="<? echo $api_key; ?>">
<input type="hidden" name="token" value="<? echo $token; ?>">
<input type="hidden" name="callback" value="<? echo $callback; ?>">

File: <input type="file" name="media"><br>

<input type="submit">
</form>



<?

// Helper to send POST requests.
function send_post_request($url,$params) {
        $postVals = '';
        foreach($params as $key=>$val)
        $postVals .= urlencode($key)."=".urlencode($val)."&";
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postVals,0,(strlen($postVals)-1)));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $return_string = curl_exec($ch);
        curl_close($ch);
        
        return $return_string;
}

?>

Callback Page:


<?
// Check for our parameters.

if( ! isset($_GET['success']) ) {
	
	die("No callback?");
	
}

$success = $_GET['success'];
$hash = $_GET['hash'];
$error = $_GET['error'];

if( $success == "0" ) {
	
	die("Error: ".$error);

}

// Grab Media Details:
$api_key = "FsUsername";
$secret = "FsSecret";
$gatekeeper = "FsGatekeeper";
$sig = md5($gatekeeper."query");

$params = array();
$params['key'] = $api_key;
$params['secret'] = $secret;
$params['sig'] = $sig;
$params['hash'] = $hash;

$media_query_string = send_post_request('http://www.framesocket.com/api/media/query.php',$params);
$media_query = json_decode($media_query_string);

if( $media_query->success == "1" ) {
	
	// Successfully fetched media info.
	echo $media_query->status_code." : ".$media_query->status_message;
	
} else {
	
	// Failed to grab media info.
	echo "Problem fetching media details: ".$media_query->error;
	
}

// Helper to send POST requests.
function send_post_request($url,$params) {
        $postVals = '';
        foreach($params as $key=>$val)
        $postVals .= urlencode($key)."=".urlencode($val)."&";
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postVals,0,(strlen($postVals)-1)));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $return_string = curl_exec($ch);
        curl_close($ch);
        
        return $return_string;
}

?>
Have a question? We are currently: