PHP Wrapper for XBMC JSON-RPC and HTTP-API

  Thread Rating:
  • 2 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
kaigoh Offline
Junior Member
Posts: 23
Joined: Jan 2009
Reputation: 0
Thumbs Up  PHP Wrapper for XBMC JSON-RPC and HTTP-API
Post: #1
Hi guys,

I needed a really easy way to do some scripting for multiple XBMC setups and found myself writing a wrapper for PHP to make life easier...

I figured it was about time I gave something back to the community, so I hope someone finds it useful!

https://github.com/kaigoh/XBMC-PHP

Cheers,

Kai.
(This post was last modified: 2011-01-03 16:21 by kaigoh.)
find quote
kaigoh Offline
Junior Member
Posts: 23
Joined: Jan 2009
Reputation: 0
Exclamation  Update...
Post: #2
Just a quick update.

I realised I didn't include instructions for actually creating the classes correctly! Whoops....

Google Code Project.

Cheers,

Kai.
find quote
alshain Offline
Senior Member
Posts: 145
Joined: Jul 2010
Reputation: 3
Post: #3
If you are using Google Code, why not upload the code to the repository? I don't want to be forced to download the code to check it out. I'd rather just browse it online.
find quote
kaigoh Offline
Junior Member
Posts: 23
Joined: Jan 2009
Reputation: 0
Post: #4
alshain Wrote:If you are using Google Code, why not upload the code to the repository? I don't want to be forced to download the code to check it out. I'd rather just browse it online.

UPDATE: Done!

Cheers,

Kai.
(This post was last modified: 2010-12-23 13:34 by kaigoh.)
find quote
alshain Offline
Senior Member
Posts: 145
Joined: Jul 2010
Reputation: 3
Post: #5
I like the idea about the dynamic __call implementation. The only hickup is, that leave all the error validation to XBMC - but this can also be considered a feature.
You're indenting is a little of at times, mixing tabs with spaces perhaps?
Why do you always pass the url along and create a new XBMCJsonRPC instance on each request? You could also use dependency-injection because you already have one from the beginning.

Not very important, but you could call the parent constructor instead of setting the url manually.

Maybe subclass Exception to provide the possibility to specifically catch your errors?

Your naming-scheme is very inconsistent. Sometimes you have abbreviations all-uppercase,s ometimes all-lowercase. Consider each abbreviation to be a single word: XbmcJsonCommand - I think this makes it much more easy to read, and it's also consistent.

You didn't set the visibility for $url in your first class.


Update

Code:
$this->remoteCommands = $this->rpc("JSONRPC.Introspect")->commands;
Does not belong into the constructor, it also doesn't desever a dedicated member variable, because you only use it in populateCommands.
Instead, make a local variable in populateCommands.

Code:
if(!class_exists($rpcCommand[0])) {
Are you sure that this is needed? I guess it means that PHP does not allow for a member variable to have the same name as an existing class? (Edit: You don't need it, see here)

Anyway, if you would like to keep naming even more consistent, you could also use lcfirst() on $rpcCommand[0] (and it's a shame PHP can't cope with the array access operator on a function call.)
(This post was last modified: 2010-12-23 14:12 by alshain.)
find quote
kaigoh Offline
Junior Member
Posts: 23
Joined: Jan 2009
Reputation: 0
Question  Update
Post: #6
UPDATE: Code updated....

Let me know what you think!!

Kai.
(This post was last modified: 2010-12-24 01:22 by kaigoh.)
find quote
alshain Offline
Senior Member
Posts: 145
Joined: Jul 2010
Reputation: 3
Post: #7
hmm.. why can't I see any differences on google code?

Not sure what you've changed, but I notice that some methods/classes are prefixed with an underscore? Why would you do that? PHP has built-in access modifiers, you do not need to use conventions as in Python to mark a method as protected/private. Case convention is still not present, and you still create an instance each time you send a command.

Maybe it's better if you discuss what you changed/didn't change or why, or I can just repeat myself Smile
find quote
kaigoh Offline
Junior Member
Posts: 23
Joined: Jan 2009
Reputation: 0
Post: #8
alshain Wrote:Maybe it's better if you discuss what you changed/didn't change or why, or I can just repeat myself Smile

Nope, no need to repeat yourself; I'm working on this in my spare time, so I'm tidying the code as and when I get a chance, but thanks for your input.

Updated xbmc-php on Google Code, you can download the newest version now.

Cheers,

Kai.
find quote
robweber Offline
Posting Freak
Posts: 885
Joined: Sep 2009
Reputation: 27
Post: #9
thanks for taking the time to work on this. I had some old PHP code from before the JSON api, but this is organized much better (i just had a few helper functions).

in any case, one feature request. I didnt take the time yet to browse your source, but the examples on the google code page say to create the class like so:

$xbmcJson = new xbmcJson('username:password@IP:Port');

would it be possible to have the constructor take an array, or a set parameter list? i dont always set a username/pass for the http server, and although I understand you need the url like that you could do the formatting in the constructor and not leave the syntax to the user,.I was thinking,

$xbmcJson = new xbmcJson(array('username'=>'user','password'=>'pass','ip'=>'127.0.0.1','port'=>80));

either way, nice work. thanks.
find quote
kaigoh Offline
Junior Member
Posts: 23
Joined: Jan 2009
Reputation: 0
Post: #10
robweber Wrote:thanks for taking the time to work on this. I had some old PHP code from before the JSON api, but this is organized much better (i just had a few helper functions).

in any case, one feature request. I didnt take the time yet to browse your source, but the examples on the google code page say to create the class like so:

$xbmcJson = new xbmcJson('username:password@IP:Port');

would it be possible to have the constructor take an array, or a set parameter list? i dont always set a username/pass for the http server, and although I understand you need the url like that you could do the formatting in the constructor and not leave the syntax to the user,.I was thinking,

$xbmcJson = new xbmcJson(array('username'=>'user','password'=>'pass','ip'=>'127.0.0.1','port'=>80));

either way, nice work. thanks.

Thanks mate,

Glad someone is making use of it!

As far as passing in the URL goes, if you are not using a username or password, you just need to pass the ip and port, 0.0.0.0:8080.

I will look at using an array in future versions though.

Thanks,

Kai.
find quote
robweber Offline
Posting Freak
Posts: 885
Joined: Sep 2009
Reputation: 27
Post: #11
thanks. even a parameter list would work. i cant remember if PHP can have multiple constructors with variable length parameter lists (like java) but it would be nice to have one constructor take username,pass,ip,port and another with just ip,port.

kaigoh Wrote:Thanks mate,

Glad someone is making use of it!

As far as passing in the URL goes, if you are not using a username or password, you just need to pass the ip and port, 0.0.0.0:8080.

I will look at using an array in future versions though.

Thanks,

Kai.
find quote
alshain Offline
Senior Member
Posts: 145
Joined: Jul 2010
Reputation: 3
Post: #12
kaigoh Wrote:Nope, no need to repeat yourself; I'm working on this in my spare time, so I'm tidying the code as and when I get a chance, but thanks for your input.

Updated xbmc-php on Google Code, you can download the newest version now.

Cheers,

Kai.
What I meant to say is, that I could not make Google show me the changes that you commited. It reported the entire file as changed, rather than individial lines, and I could not see any of the "major" things implemented that I noted.

You are doing something wrong. Somehow you remove old revisions of the files. I cannot view any old revisions on google code... maybe it's an error on their side, but it's really peculiar. If you click on "Changes", then "r5", it says "Files are empty". Maybe it is, because you've put a file into the branches directory, your development should take place inside the trunk folder, or inside a subfolder of branches, but not in the branches folder itself, it has a special meaning.

It just makes it very difficult to spot what you've changed. I can see however, that you now use dependency injection and now make use of parent::.

As for the constructor: PHP does not support multiple constructors. However, as it is dynamically and weakly typed, you can pass anything to the constructor that you'd like. It can be adjusted to recognize several constructor "argument formats". You can use func_get_args() to get all passed arguments.
find quote
kaigoh Offline
Junior Member
Posts: 23
Joined: Jan 2009
Reputation: 0
Post: #13
robweber Wrote:thanks. even a parameter list would work. i cant remember if PHP can have multiple constructors with variable length parameter lists (like java) but it would be nice to have one constructor take username,pass,ip,port and another with just ip,port.

Done!

See the new version at Google Code.

Cheers,

Kai.
find quote
alshain Offline
Senior Member
Posts: 145
Joined: Jul 2010
Reputation: 3
Post: #14
Can you throw a custom Exception instead? That way one can make sure we are catching an exception that your code generated.

Also, the method "hostAlive" might be renamed to "isHostAlive" to indicate the return of a boolean.


I see you have lots of duplicate code in the HTTP and JSON part. You can make use of inheritance and method overrides: you can make the parent constructor invoke a method "parseConfig" for instance, and put all your shared config handling in there. Then you make a method assertLiveConnection() in which you put the code that would throw an error.

Now, the HTTP's constructor shouldn't be needed anymore, but in the JSON constructor, you'll put the populateCommands thing.


Now you moved the code into the root directory, not the trunk Smile
find quote
robweber Offline
Posting Freak
Posts: 885
Joined: Sep 2009
Reputation: 27
Post: #15
kaigoh Wrote:Done!

See the new version at Google Code.

Cheers,

Kai.

awesome. thanks alot. I'm looking forward to using this soon!
find quote
Post Reply