Camideo Video Search API - PHP Example

 
 
In this tutorial we will try to show you how to use our Camideo Video Search API and it's JSON feed using PHP.
 
Technologies used in this article:
  • PHP

  • HTML

In this example we are going to parse video feed from

YouTube, Vimeo, MySpace, Dailymotion, MetaCafe, SoundCloud

. For more detailed description about

Camideo Video Search JSON feed

you may read our Camideo Video Search API documentation.
 

1. Create the Search Form in HTML

 
First we have to create a HTML page where we will take the inputs. There are two inputs that we are going to take from user: Search Criteria and Source.
					<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
					<html>
					 <head>
					  <title> New Document </title>
					  <meta name="Author" content="">
					  <meta name="Keywords" content="">
					  <meta name="Description" content="">
					 </head>

					 <body>
					  <form method="get" action="search-camideo-video-api.php">
						<div>Keywords : <input type="text" name="q"></div>
						<div>Source   : <select name="source">
											<option value="youtube">YouTube</option>
											<option value="vimeo">Vimeo</option>
											<option value="myspace">MySpace</option>
											<option value="metacafe">MetaCafe</option>
											<option value="dailymotion">DailyMotion</option>
											<option value="soundcloud">SoundCloud</option>
										</select>
						</div>
						<div><input type="submit" value="Search">
					  </form>
					 </body>
					</html>
				
And the out put will look like:
Home Page
 

2. Create the PHP file to call Camideo API

 
You might need a API key to make a call. Please use our Key Generation Tool to get one. Once you have that you can make a call to our API. So our search-camideo-video-api.php will look like:
					<?php

						$q = htmlspecialchars($_GET["q"]);
						$source = htmlspecialchars($_GET["source"]);

						//let's build the url to call. We will assume that we are going to fetch first page results. 
						// You can take page number as input parameter or may be use you own logic to determine.

						$url = 'http://api.camideo.com/?key=API_KEY&source=' . $source . '&q=' 
								. $q . '&response=json&page=1' ;

						//Now we are going to make the call. It's simple.

						$json = file_get_contents($url); 

						//We have the output as JSON string, let's convert it to a JSON object

						$data = json_decode($json, TRUE);

						?>
				
 

3. Cheking for error in JSON Output

 
We might have encountered an error in our call. So it's better to check for error and redirect it to error file in case of error. We will add these lines at the end of our search-camideo-video-api.php
					if(!$data){
						header("Location:error.php");
					}else if($data['Camideo']['Error']){
							header("Location:error.php?code=" . $data['Camideo']['Error']['code'] . "&desc=" . $data['Camideo']['Error']['description']);
					}
				
Our error.php will be like:
					<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
					<html>
					 <head>
					  <title> New Document </title>
					  <meta name="Generator" content="">
					  <meta name="Author" content="">
					  <meta name="Keywords" content="">
					  <meta name="Description" content="">
					 </head>

					 <body>
					  <?php if ($_GET["code"] == "1000"){ ?>
						<div>It looked like we made a mess with the API key.We are fixing this at the earliest. Sorry for the inconveniences.</div>
						<?php }else if ($_GET["code"] == "1001"){ ?>
						<div>It looked like you were playing with the URL and mis-spelled the response type. Currently we only support JSON response.</div>
						<?php }else if ($_GET["code"] == "1002"){ ?>
						<div>It looked like you were playing with the URL and mis-spelled the source. Currently we only support YouTube, Vimeo, MySpace, Dailymotion, MetaCafe and SoundCloud.</div>
						<?php }else if ($_GET["code"] == "1003"){ ?>
						<div>It looked like you were playing with the URL and missed the search criteria. </div>
						<?php }else if ($_GET["code"] == "1004"){ ?>
						<div>It looked like we made a mess with the searching logic.We are fixing this at the earliest. Sorry for the inconveniences.</div>
						<?php }else { ?>
						<div>It looked like we made a mess.We are fixing this at the earliest. Sorry for the inconveniences.</div>
						<?php }?>
					 </body>
					</html>

				
 

4. Everything is fine. Let's parse the feed

 
Now we have error free feed. Let's modify our search-camideo-video-api.php to show the videos. We will append these lines at the bottom of the file.
					else{

						foreach($data['Camideo']['videos'] as $entry) { 
					?>
						<div class="thumb">
							<?php if($source == "metacafe" || $source == "myspace"){ ?>
							<embed flashVars="playerVars=autoPlay=no" src="<?php echo $entry['embedCode']; ?>" width="390" height="290" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" name="Metacafe_<?php echo $entry['videoId']; ?>" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>
							<?php } else { ?>
							<iframe type="text/html" width="390" height="290" src="<?php echo $entry['embedCode']; ?>"
								frameborder="0"></iframe>
							 <?php } ?>
						</div> <!-- thumb ends -->
						<div class="videoDetails">
							<div class="videoTitle">
								<a target="_blank" href="<?php echo $entry['link']; ?>" title="<?php echo $entry['title']; ?>" rel="nofollow">
									<h2><?php echo $entry['title']; ?></h2>
								</a>
							</div>
						<div class="videoDesc">
							<?php echo $entry['description']; ?>
						</div>
						<div>
							Uploaded by 
								<a class="videoAuthor" href="<?php echo $entry['authorLink']; ?>" rel="nofollow" target="_blank"title="<?php echo $entry['author']; ?>">
									<?php echo $entry['author']; ?>
								</a> | 
							Length 
								<span class="videoWhiteText">
									<?php echo $entry['duration']; ?>
								</span> | 
								<span class="videoWhiteText">
									<?php echo number_format($entry['views']); ?>
								</span> views
						</div>

					<?php
						}
					}

					?>
				
 
 

5. Download Source Code