Skip to content
Browse files

Frodo compatibility, debug mode

- basic compatibility with API of XBMC Frodo (did not care about backwards compatibility)
- added debugging method and use it in the request (logging can be enabled with setDebug(true))
  • Loading branch information...
1 parent 505d362 commit 68c35f0213ad8f95795f6915b4b000cddea976d5 @amenk committed Apr 8, 2013
Showing with 73 additions and 36 deletions.
  1. +25 −20 exampleMovieWall.php
  2. +48 −16 xbmc.php
View
45 exampleMovieWall.php
@@ -19,34 +19,39 @@
$movies = array();
$moviesHTML = "";
-foreach($moviesRaw->movies as $movie) {
- //Removes "The" from the movies title, for sorting reasons.
- $title = str_replace("The ", "", $movie->label);
- $movies[$title]['id'] = $movie->movieid;
- $movies[$title]['title'] = $movie->label;
- $movies[$title]['filename'] = $movie->file;
- /*
- We need to check certain properties exist before referencing them,
- otherwise a notice will be generated by PHP due to a JSON-RPC error.
- */
- if(property_exists($movie, 'thumbnail'))
- $movies[$title]['thumbnail'] = $movie->thumbnail;
- if(property_exists($movie, 'fanart'))
- $movies[$title]['fanart'] = $movie->fanart;
-}
+if (!isset($moviesRaw->movies))
+{
+ $moviesHTML = 'No movies found';
+} else {
+ foreach($moviesRaw->movies as $movie) {
+ //Removes "The" from the movies title, for sorting reasons.
+ $title = str_replace("The ", "", $movie->label);
+ $movies[$title]['id'] = $movie->movieid;
+ $movies[$title]['title'] = $movie->label;
+ $movies[$title]['filename'] = $movie->file;
+ /*
+ We need to check certain properties exist before referencing them,
+ otherwise a notice will be generated by PHP due to a JSON-RPC error.
+ */
+ if(property_exists($movie, 'thumbnail'))
+ $movies[$title]['thumbnail'] = $movie->thumbnail;
+ if(property_exists($movie, 'fanart'))
+ $movies[$title]['fanart'] = $movie->fanart;
+ }
//Sort the array by movie name (XBMC function that is supposed to do this dosn't seem to do anything!!!)
-ksort($movies);
+ ksort($movies);
//Display the "movie-wall"
-foreach($movies as $movie) {
- $moviesHTML .= "<img src=\"http://".$xbmcHost->url()."/".$xbmcJson->Files->Download($movie['thumbnail'])->path."\" width=\"104\" height=\"156\" title=\"".$movie['title']."\">";
+ foreach($movies as $movie) {
+ $moviesHTML .= "<img src=\"http://".$xbmcHost->url()."/".$xbmcJson->Files->Download($movie['thumbnail'])->path."\" width=\"104\" height=\"156\" title=\"".$movie['title']."\">";
+ }
}
//Output some HTML
?>
<html>
- <head>
+<head>
<title>My XBMC Movie Collection</title>
<style type="text/css">
body {
@@ -58,4 +63,4 @@
<body>
<?php echo $moviesHTML; ?>
</body>
-</html>
+</html>
View
64 xbmc.php
@@ -167,7 +167,7 @@ public function __construct($config) {
throw new xbmcError('Bad URL parameters');
}
}
-
+
private function isHostAlive($host, $port = "8080") {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@@ -207,6 +207,18 @@ class xbmcJsonRPC {
private $_url;
+ protected $_debug = false;
+
+ public function setDebug($debug)
+ {
+ $this->_debug = $debug;
+ }
+
+ public function getDebug()
+ {
+ return $this->_debug;
+ }
+
public function __construct() {
}
@@ -218,30 +230,50 @@ public function setUrl($url) {
public function getUrl() {
return $this->_url;
}
-
+
+ protected function debug($message) {
+ if (!$this->getDebug()) {
+ return;
+ }
+ error_log($message, 0);
+ }
+
public function rpc($method, $params = null) {
-
$uid = rand(1, 9999999);
$json = array(
'jsonrpc' => '2.0',
'method' => $method,
- 'params' => $params,
- 'id' => $uid
- );
+ 'id' => $uid
+ );
+ if (!empty($params)) {
+ $json['params'] = $params;
+ }
$request = json_encode($json);
-
- $ch = curl_init();
+
+ $url = "http://".$this->_url."/jsonrpc?" . $method;
+ $this->debug(sprintf('Request to URL "%s": %s', $url, var_export($request, true)));
+
+ $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, "http://".$this->_url."/jsonrpc");
+ curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
- $responseRaw = curl_exec($ch);
-
- $response = json_decode($responseRaw);
+ curl_setopt($ch, CURLOPT_FAILONERROR, true);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
+
+ $responseRaw = curl_exec($ch);
+
+ if ($responseRaw === false) {
+ throw new xbmcError('cURL Error: ' . curl_error($ch));
+ }
+
+ $response = json_decode($responseRaw);
+
+ $this->debug('Response: ' . var_export($response,true));
- if ($response->id != $uid) {
+ if ($response->id != $uid) {
throw new xbmcError('JSON-RPC ID Mismatch');
}
@@ -265,12 +297,12 @@ class xbmcJson extends xbmcJsonRPC {
public function __construct(xbmcHost $xbmcHost) {
parent::setUrl($xbmcHost->url());
- $this->populateCommands($this->rpc("JSONRPC.Introspect")->commands);
+ $this->populateCommands($this->rpc("JSONRPC.Introspect")->methods);
}
private function populateCommands($remoteCommands) {
- foreach($remoteCommands as $remoteCommand) {
- $rpcCommand = explode(".", $remoteCommand->command);
+ foreach($remoteCommands as $command=>$remoteCommand) {
+ $rpcCommand = explode(".", $command);
if(!class_exists($rpcCommand[0])) {
$this->$rpcCommand[0] = new xbmcJsonCommand($rpcCommand[0], parent::getUrl(), $this);
}

0 comments on commit 68c35f0

Please sign in to comment.
Something went wrong with that request. Please try again.