DrivecastAPI 1.0 PHP Library
From DriveCast Wiki
DrivecastAPI 1.0 PHP Library
Overview
This is the implementation of a library, using the programming language PHP, that allows to fetch data from the Drivecast API.
Here you can find the actual implementation of the DrivecastAPI 1.0 PHP Class.
How it works
The PHP implementation consists of an object called DrivecastAPI, which provides one user method: handleRequest.
The code to create a DrivecastAPI object reference is:
include("DrivecastAPI.php");
$api = new DrivecastAPI("username","password");
$api->setAPIkey("you APIkey");
You can get your personal APIkey under the Software menu, Developer tools submenu section of your Drivecast account.
Make a request
To make a request call the method handleRequest
$api->handleRequest('feed','read');
The function handleRequest returns true or false, according to the request result.
Fetch the results and the request status
The instance of the DivecastAPI Object, contains three variables.
$api->statuscode // contains the HTTP Response code (200, 400 etc)
$api->statusdesc // contains the reason phrase of the respnse (like OK, Unhautorized)
See the Drivecast_API reference, for the complete list of these codes
$api->result // if there are attachment data, it is the standard Class Object containing all the responses in a key/value array, otherwise it is empty
Here is a simple way to print the results of the request.
if($api->handleRequest('feed','read')){ print_r($api->statuscode); echo "<br>"; print_r($api->statusdesc); echo "<br>"; print_r($api->result); }
Next some examples:
Request the feed
Request the feed of your Drivecast libraries
$api->handleRequest('feed','read');
If you need to get the device feed, with a serial (not so usual, you must add to Drivecast two Devices with the same name to obtain a serial associated to the Device)
$api->handleRequest('feed/deviceName/serial');
Create an empty playlist
$api->handleRequest('playlist/MyPlaylist','create');
Delete a playlist
$api->handleRequest('playlist/MyPlaylist','delete');
Get a particular library element
$api->handleRequest('library/elementID','read');
Modify the metadata associated to a library element
$obj = new LibraryItem("Mofified Author","Modified Title","Modified Description");
$api->handleRequest('library/elementID','modify',$obj);
The LibraryItem object structure is:
LibraryItem("author","title","description")
You can also set the variables after the instance of the object
$obj = new LibraryItem(); $obj->author = "author"; $obj->title = "title"; $obj->description = "description";
Create a new record
$obj = new Recording("KossuthRadio","radio","2009-05-24 10:11:12","2009-05-24 11:10:12","weekly","on cloud","2","new record");
$api->handleRequest('recording','create',$obj);
The structure of the Recording object is:
Recording("radioID","timezone_used","from_time","to_time","repeat","rectype","keep","title")
Update a record
$obj = new Recording("KossuthRadio","radio","2009-05-24 10:11:12","2009-05-24 11:10:12","weekly",on cloud",2","modified recording title"}';
$api->handleRequest('recording/elementID','modify',$obj);
Get all podcasts ID
$api->handleRequest('podcast','read');
Get a podcast
$api->handleRequest('podcast/elementID','read');
Delete a podcast
$api->handleRequest('podcast/elementID','delete');
Insert a podcast
$obj = new Podcast(); $obj->podcast_url = "http://rss.cnn.com/services/podcasting/newscast/rss.xml"; $obj->podcast_title = "Podcast Title"; $obj->podcast_description = "Podcast Description"; $api->handleRequest('podcast','create',$obj);
The structure of the Podcast object is:
Podcast("podcast_url","podcast_title","podcast_description")
Update a podcast
$obj = new Podcast(); $obj->podcast_url="http://rss.cnn.com/services/podcasting/newscast/rss.xml"; $obj->podcast_title="Podcast Title Modified"; $obj->podcast_description="Podcast Description MODIFIED"; $api->handleRequest('podcast/elementID','modify',$obj);
Get a Thumbnail
$api->handleRequest('thumbnail/elementID','read');
Where elementID is a library UID.
In thi case, $api->statuscode and $api->statusdesc are empty, while $api->result contains the image data.
How to write a Thumbnail to a file in PHP
Here is a simple example on how to write the thumbnail returned by the PHP library, into a file
if($api->handleRequest('thumbnail/elementID','read')){
$imageData = $api->result;
$imageFile = "thumbnail.jpg";
$fh = fopen($imageFile, 'wa+') or die("can't open file");
fwrite($fh, $imageData);
fclose($fh);
}
You must have write permission in the directory on which you want to write the image file.
