Skip to Content
It is currently December 10th, 2023, 5:55 pm

All times are UTC - 6 hours [ DST ]




 [ 38 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Generate "API" using PHP
PostPosted: December 11th, 2012, 3:11 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Is there any way to generate a plaintext "API" with info like this, using PHP?

(!empty($user->lang['QUOTE'])) ? $user->lang['QUOTE'] : ucwords(strtolower(str_replace('_', ' ', 'QUOTE'))):
{"type":"currentsong","data":{"art":"http://url.com","artist":"Artist Name","song":"Song Name","history":[{"artist":"Artist Name","song":"Song Name","id":Song ID number,"art":"http://url.com"},{"artist":"Artist Name","song":"Song Name","id":Song ID number,"art":"http://url.com"}]}}


Thanks!

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
 
PostPosted: December 11th, 2012, 3:23 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
Do you need a full RESTful API to the SAM database or do you just need to serve JSON for the currently playing song?

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
 
PostPosted: December 11th, 2012, 3:53 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Frankly, I don't know much about APIs (RESTful or not), and in theory, having a full API would probably be nice, if there's a way to do that, but if not, getting the JSON for the current song should work too.
Thanks!

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
 
PostPosted: December 11th, 2012, 5:36 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
RESTful usually refers to an HTTP Method based API.

You issue HTTP GET to (who thought it) GET information,
HTTP POST to CREATE new data,
HTTP PUT is usually used to change existing data (or to create new data with a specific identifier like the songID)
HTTP DELETE to (again surprising) delete information.

That's basically how all modern APIs like Twitter, Facebook etc. work.
They just include an advanced authentication and permission scheme on top of that.
The data returned is usually either JSON or XML.

With that said, there are Frameworks in many programming languages that enable one to easily build a webservice based on a MySQL database.
Of course one could still build such a thing from scratch, but that's only really useful if you just need a single page without authentication.

(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
<?php
// Define path to application directory
defined('APP_PATH') || define('APP_PATH', realpath(dirname(__FILE__)));
// Ensure common is on include_path
set_include_path(implode(PATH_SEPARATOR, array(realpath(APP_PATH . '/library'), '.')));
// Make a shortcut to DIRECTORY_SEPARATOR
define('DS', DIRECTORY_SEPARATOR);

// The Singleton Database class
require_once('Common/Database.php');

$db = Database::getInstance(APP_PATH.DS.'config'.DS.'dbconfig.xml.php');

$db->setFetchMode(Zend_Db::FETCH_OBJ);

// show only the 20 most recent results
$select = $db->select()->from('historylist')->order('id DESC')->limit(0, 20);
$stmt = $db->query($select);
$result = $stmt->fetchAll();

header('Content-Type: application/json');
echo json_encode($result);
?>


Place this in the root of SAMPHPWeb so it can use your dbconfig.xml.php and the Zend DB stuff.

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
 
PostPosted: December 11th, 2012, 5:47 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Thanks for the info.
I only really need to retrieve info at this point.
So this will show the past 20 items in the history list?
And is there any way to edit which info it shows, or to filter things based on an item in the database, like to remove anything not set to songtype S?
Thanks a lot!

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Last edited by isaacl on December 11th, 2012, 5:55 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: December 11th, 2012, 5:52 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
http://framework.zend.com/manual/1.12/e ... ding.where

Filtering by songtype is easy. WHERE songtype = 'S' or WHERE songtype != 'S'
Filtering by category is harder, that involves a JOIN across 3 tables. (I.e. Only possible with Advanced Database Knowledge)

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
 
PostPosted: December 11th, 2012, 5:55 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Will look...
And I meant songtype S, not category - edited above.
Also, I tried changing this:
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
$select = $db->select()->from('historylist')->limit(20);

to this:
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
$select = $db->select()->from('historylist')->limit(1);

since I wanted to only get the currently playing song (at least for now), but it seems to be displaying a track that played a while ago.
Any idea what the issue is?
Thanks!

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
 
PostPosted: December 11th, 2012, 6:00 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Seems like it's getting the first row that way.
Supposedly something like this should work:
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
SELECT fields FROM table ORDER BY id DESC LIMIT 1;

But I have no clue how to do that with the Zend syntax...

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
 
PostPosted: December 11th, 2012, 6:04 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Never mind, a few searches online later, this seems to work:
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
$select = $db->select()->from('historylist')->order('id DESC')->limit(1);

Maybe edit the original code, so that this works for everyone...
Thanks!

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
 
PostPosted: December 11th, 2012, 6:14 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
For clarity you should probably write down the limit in zend with both parameters.
Don't ask me why Zend does reverse the order of the parameters, but with 2 paramters the LIMIT statement defines the OFFSET and LIMIT in a single command (MySQL has 2 ways of writing this: LIMIT 0,20 <=> LIMIT 20 OFFSET 0 {NOTE: the parameters are reversed here}).
I.e.: Limit 0, 20 will show the first 20, but Limit 20,1 will show 1 single row, but leave out the first 20.

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
 
PostPosted: December 11th, 2012, 6:17 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
OK... How would I do that? ;)

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
 
PostPosted: December 11th, 2012, 6:22 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Also, if I only want to show specific items in each row, like title, artist, album, art, etc, how would I do that?
And it seems like the BUYCD URL isn't showing up using this method - is there anything that would be messing that up?

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
 
PostPosted: December 11th, 2012, 6:27 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
http://framework.zend.com/manual/1.12/e ... ng.columns
Add columns to the second argument of from in array form.

Are you sure the BUYCD doesn't show up or is it maybe just empty?
Also it could be that you are looking at an old record from BEFORE you entered the BuyCD info.
The historylist stores all the song details itself and does not neccessarily reflect the current state of the songlist table.

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
 
PostPosted: December 11th, 2012, 6:38 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Never mind, got that too:
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
$select = $db->select()->from('historylist',array('title', 'trackno', 'artist', 'album', 'picture', 'buycd'))->order('id DESC')->limit(1);

This seems to work.
And the BUYCD URL worked - it had extra slashes in there, which could have been resolved in PHP 5.4 with JSON_UNESCAPED_SLASHES, but I am running 5.3 on my server, so this worked:
(!empty($user->lang['CODE'])) ? $user->lang['CODE'] : ucwords(strtolower(str_replace('_', ' ', 'CODE'))):
echo str_replace('\\/', '/', json_encode($result));

For some reason, the initial track didn't display the BUYCD link at all, but that could have been a problem with that track.

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Last edited by isaacl on December 11th, 2012, 6:42 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: December 11th, 2012, 6:42 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
(!empty($user->lang['QUOTE'])) ? $user->lang['QUOTE'] : ucwords(strtolower(str_replace('_', ' ', 'QUOTE'))):
Never mind, got that too:

What exactly do you need me for, then? :D

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
 
Display posts from previous:  Sort by  
 [ 38 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group