Sample Scripts API wrappers in all popular languages

{count_titles}

 
VBScript sample
This sample script allows to get a list of user's media:
Dim xml, sURL, sRequest, result, userID, userKey   

    ' put your real userID and userKey below
    userID = "0"
    userKey = "your_key"

    xml = "<?xml version=""1.0""?>" & VbCrLf
    xmlxml = xml & "<query>" & VbCrLf
    xmlxml = xml & "<userid>" & userID & "</userid>" & VbCrLf
    xmlxml = xml & "<userkey>" & userKey & "</userkey>" & VbCrLf
    xmlxml = xml & "<action>" & "GetMediaList" & "</action>" & VbCrLf
    xmlxml = xml & "</query>" & VbCrLf

    sUrl = "http://manage.encoding.com"
    sRequest = "xml=" & xml
    result = HTTPPost(sUrl, sRequest)

    Function HTTPPost(sUrl, sRequest)
        Set oHTTP = CreateObject("Microsoft.XMLHTTP")
        oHTTP.open "POST", sUrl,false
        oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        oHTTP.setRequestHeader "Content-Length", Len(sRequest)
        oHTTP.send sRequest
        HTTPPost = oHTTP.responseText
    End Function

PHP sample
This sample script allows user to enter parameters and send API request for encoding:
<?php
function sendRequest($xml)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://manage.encoding.com/");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=" . urlencode($xml));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    return curl_exec($ch);
} 


// Begin processing User's POST
if(!empty($_POST['source']))
{
    // Preparing XML request
  
    // Main fields
    $req = new SimpleXMLElement('<?xml version="1.0"?><query></query>');
    $req->addChild('userid', MY_ID);
    $req->addChild('userkey', MY_KEY);
    $req->addChild('action', 'AddMedia');
    $req->addChild('source', $_POST['source']);
  
    $formatNode = $req->addChild('format');
    // Format fields
    foreach($_POST['format'] as $property => $value)
    {
        if ($value !== '')
        $formatNode->addChild($property, $value);
    }
  
    // Sending API request
    $res = sendRequest($req->asXML());
  
    try
    {
        // Creating new object from response XML
        $response = new SimpleXMLElement($res);
      
        // If there are any errors, set error message
        if(isset($response->errors[0]->error[0])) {
            $error = $response->errors[0]->error[0] . '';
        }
        else
        if ($response->message[0]) {
            // If message received, set OK message
            $message = $response->message[0] . '';
        }
    }
    catch(Exception $e)
    {
        // If wrong XML response received
        $error = $e->getMessage();
    }
  
    // Displaying error if any
    if (!empty($error)) {
        echo '<div class="error">' . htmlspecialchars($error) . '</div>';
    }
  
    // Displaying message
    if (!empty($message)) {
        echo '<div class="message">' . htmlspecialchars($message) . '</div>';
    }
    exit;
}
?>
Java sample
import java.io.*;
import java.net.*;

public class EncodingTest {

    private static int startEncodingWorkflow() {
        // replace ID and key with your own
        String userID = "ID";
        String userKey = "key";
        String mediaID = "1";
        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version='1.0'?>");
        xml.append("<query>");
        xml.append("<userid>"+userID+"</userid>");
        xml.append("<userkey>"+userKey+"</userkey>");
        xml.append("<action>GetMediaInfo</action>");
        xml.append("<mediaid>"+mediaID+"</mediaid>");
        xml.append("</query>");

        URL server = null;

        try {
            String url = "http://manage.encoding.com";
            System.out.println("Connecting to:"+url);
            server = new URL(url);

        } catch (MalformedURLException mfu) {
            mfu.printStackTrace();
            return 0;
        }

        try {
            String sRequest = "xml=" + URLEncoder.encode(xml.toString(), "UTF8");
            System.out.println("Open new connection to tunnel");
            HttpURLConnection urlConnection = (HttpURLConnection) server.openConnection();
            urlConnection.setRequestMethod( "POST" );
            urlConnection.setDoOutput(true);
            urlConnection.setConnectTimeout(60000);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            BufferedWriter out = new BufferedWriter( new OutputStreamWriter( urlConnection.getOutputStream() ) );
            out.write(sRequest);
            out.flush();
            out.close();
            urlConnection.connect();
            InputStream is = urlConnection.getInputStream();
            String str = urlConnection.getResponseMessage();
            System.out.println("Response:"+urlConnection.getResponseCode());
            System.out.println("Response:"+urlConnection.getResponseMessage());
            StringBuffer strbuf = new StringBuffer();
            byte[] buffer = new byte[1024 * 4];

            try {
                int n = 0;
                while (-1 != (n = is.read(buffer))) {
                    strbuf.append(new String(buffer, 0, n));
                }

                is.close();

            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

            System.out.println(strbuf.toString()); 

        } catch (Exception exp) {
            exp.printStackTrace();
        }

        return 0;
    }

    public static void main (String[] args) {
        startEncodingWorkflow();
    }
}
CFM sample
You can use the following code as a sample:
<!-- Building our query -->
<cfsavecontent variable="xml">
<?xml version="1.0" ?>
<query>
  <action>AddMedia</action>
  <userid>0</userid>
  <userkey>your_key</userkey>
  <source>http://your.server.tld/some/path/file</source>
  <format>
   <output>3gp</output>
  </format>
  <format>
   <output>wmv</output>
  </format>
  <format>
   <output>flv</output>
  </format>
</query>
</cfsavecontent>

<!-- Setting up the URL to send the request -->
<cfset theURL = "http://manage.encoding.com">

<!-- Sending the request -->
<cfhttp url="#theURL#" charset="utf-8" method="post">
<cfhttpparam type="formfield" name="xml" value="#xml#">
</cfhttp>

<!-- Getting the result -->
<cfdump var="#cfhttp.fileContent#">
C# sample
static string HTTPPost(string sUrl, string sRequest)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = sRequest.Length;
    request.GetRequestStream().Write(Encoding.UTF8.GetBytes(sRequest), 0, sRequest.Length);
    request.GetRequestStream().Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string result = reader.ReadToEnd();
    reader.Close();
    return result;
}

static void Main_Encoding(string origin, string destination)
{
    string xml, sUrl, sRequest, result, userID, userKey;
    //put your real userID and userKey below
    userID = "0";
    userKey = "your_key";
    xml = string.Format(
@"<?xml version=""1.0""?>

<query>
<userid>{0}</userid>
<userkey>{1}</userkey>
<action>AddMedia</action>
<source>{2}</source>
<format>
<output>flv</output>
<destination>{3}</destination>
</format>
</query>", userID, userKey, origin, destination);

    sUrl = "http://manage.encoding.com/";
    sRequest = "xml=" + HttpUtility.UrlEncode(xml);
    result = HTTPPost(sUrl, sRequest);
}

static void Main(string[] args)
{
    string origin = "http://yoursite.com/video/movie.avi";
    string destination = "ftp://username:password@yourftphost.com/video/encoded/test.flv";
    Main_Encoding(origin, destination);
}