Please log in
 

Upload in One Step with PHP

Introduction

The purpose of this tutorial is to show you how to upload a video with an API method and PHP. This upload is made on the server side.

For this tutorial, we will use the upload/uploadVideo method.

For each API method that we use in this tutorial, we create a method in the "functions.php" file that allows you to call an API method thanks to the Curl class and to return a response in the XML format.

For the "Curl.php" class, you can find a presentation in the following article: The Curl class.

This tutorial consists of three steps:

  • token initialization.
  • form preparation.
  • upload script.

Tokens initialization

To use the upload/uploadVideo method, it is important to firstly generate the application token (appToken) and the authentication token (authToken).

There are several ways to generate it. Additional information can be found in: Generate appToken.

In this example we use the sessions that can store the appToken and the authToken. This is done in the 'init.php' file.

We can get the two tokens on each PHP file by doing the following:


$appToken = $_SESSION['appToken'];
$authToken = $_SESSION['authToken'];

Form preparation

The form allows you to retrieve the video metadata and the video source file that you wish to upload.

You'll find a list of metadata in the following article: Adding video.

In our example, we have five fields that are used to retrieve the following data:

  • Title
  • Description
  • Categories
  • Keywords
  • Source file

Here is the HTML form used in the example:


<form action="uploadVideo.php" method="post" enctype="multipart/form-data" name="form1">
  <p>Title : <input type="text" name="title" /></p>
  <p>Description : <textarea rows="3" name="description">Description</textarea></p>
  <p>Categories :</p>
  <!--  Insertion du script PHP de récupération des catégories  -->
  <p>Keywords : <input type="text" name="keywords" /></p>
  <p>Video File : <input type="file" name="video_file" /></p>
  <input type="submit" name="Submit" value="Envoyer">
</form>

For categories, we use the video/getCategoryList method to display the list of categories associated with your product on the form.


$categories = get_category_list($appToken);

$max = 5;
$cpt = 0;

foreach ($categories->categories->category as $category) {
	if ($cpt >= $max) {
		echo "<br />";
		$cpt = 0;
	}
	echo "<input type=\"checkbox\" name=\"categories[]\" value=\"$category\"> $category";
	$cpt++;
}

The form sends the data to the "uploadVideo.php" script that we will now study.

Upload script

The upload script allows us to retrieve the data from the form.

To retrieve the metadata, use the following code:


$title = $_POST['title'];
$description = $_POST['description'];
$categories = $_POST['categories'];
$keywords = $_POST['keywords'];

From the retrieved metadata we can build the $xml_data variable that represents the video metadata in XML format and that will be sent with the upload/uploadVideo API method.

The $categories variable and the $keywords variable are treated to match with the XML format required by the method.


$xml_data = "<kewego_call>
  <params>
    <title>$title</title>
    <description>$description</description>
    <categories>
      $categories
    </categories>
    <keywords>
      $keywords
    </keywords>
    <language>fr</language>
    <country>fr</country>
    <postal_code>28000</postal_code>
    <access>public</access>
    <commentable>no</commentable>
    <shareable>no</shareable>
    <notable>no</notable>
    <production_date>2008-01-01 12:00:00</production_date>
  </params>
</kewego_call>";

We then retrieve the source file and copy it temporarily on the server thanks to the move_uploaded_file() PHP method:


$filename = $_FILES['video_file']['name'];
$destination_path = DESTINATION_PATH;
$video_file = $destination_path.$filename ;
move_uploaded_file($_FILES['video_file']['tmp_name'], $video_file);

Now that we have all pamameters, we use the upload_video() method: 


function upload_video($appToken, $authToken, $video_file, $xml_data, &$error) {
	$response = APICall()->postFile('http://upload.kewego.com/api/uploadVideo/index.php', array('appToken' => $appToken, 'token' => $authToken, 'file' => "@".$video_file, 'xml_data' => $xml_data), $info);
	
	// if cURL throw an error
	if ($info['http_code'] == 0) {
		$error = new ErrorVO("Curl", APICall()->getLastError());
		return;
	}
	
	// convert the response into an Object
	$xml_message = simplexml_load_string($response);
	
	// if Kewego throw an error
	if ((string)$xml_message->attributes()->error == "yes") {
		$error = new ErrorVO("Kewego ".$xml_message->kewego_error->attributes()->errno, $xml_message->kewego_error);
		return;
	}
	
	// return the SIG of uploading video
	$sig = $xml_message->message->sig;
	return $sig;
}

This method returns the SIG of the uploaded video.

Before the $video_file parameter (that represents the file source URL) we added the "@" character. This is a standard of cURL when making a file treatment.

The $error parameter allows the developer to retrieve the error information when this happens.


$sig = upload_video($appToken, $authToken, $video_file, $xml_data, $error);

if (isset($error)) {
	die("Error ".$error->code." : ".$error->message);
}

unlink($video_file);

Finally don't forget to delete the temporary file by using the unlink() PHP method.

Download the code sample download