Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Created a XBMC JSON Test harness

  • Loading branch information...
commit 71c154c77c0a4504b19c58ad4c1b02d726457d5b 1 parent 270a4b1
rodneyshupe rodneyshupe authored
Showing with 71 additions and 18 deletions.
  1. +33 −15 jsonservice.php
  2. +38 −3 xbmcjsonlib.php
48 jsonservice.php
View
@@ -9,24 +9,42 @@ function error_array($code, $message) {
$jsonRequest = urldecode(file_get_contents("php://input"));
$arrRequest = json_decode($jsonRequest, true);
-if(!empty($arrRequest)) {
- switch ($arrRequest["method"]) {
- case "SaveLayout":
- $arrResult = save_layout($arrRequest);
- break;
- case "GetWidgets":
- $arrResult = get_widgets($arrRequest);
- break;
- case "XBMCPassthough":
- $arrResult = xbmc_passthough($arrRequest);
- break;
- default:
- $arrResult = error_array(-32601, "Method not found.");
+if(!empty($DEBUG) && $DEBUG && !empty($arrRequest['jsonrpc']) && ($arrRequest['jsonrpc'] == "2.0") && !empty($xbmcjsonserviceoverride)) {
+ // Use XBMC test harness
+ if(!empty($arrRequest['method']) && file_exists($arrRequest['method'])) {
+ $response = file_get_contents($arrRequest['method']);
+ } else {
+ //json rpc call procedure
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_POST, 1);
+ curl_setopt($ch, CURLOPT_URL, $xbmcjsonserviceoverride);
+
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonRequest);
+ $response = curl_exec($ch);
+ curl_close($ch);
}
+ echo $response;
} else {
- $arrResult = error_array(-32700, "Parse error.");
+ if(!empty($arrRequest)) {
+ switch ($arrRequest["method"]) {
+ case "SaveLayout":
+ $arrResult = save_layout($arrRequest);
+ break;
+ case "GetWidgets":
+ $arrResult = get_widgets($arrRequest);
+ break;
+ case "XBMCPassthough":
+ $arrResult = xbmc_passthough($arrRequest);
+ break;
+ default:
+ $arrResult = error_array(-32601, "Method not found.");
+ }
+ } else {
+ $arrResult = error_array(-32700, "Parse error.");
+ }
+ echo json_encode($arrResult);
}
-echo json_encode($arrResult);
function save_layout($arrRequest) {
if(!empty($arrRequest["params"]) && is_array($arrRequest["params"])) {
41 xbmcjsonlib.php
View
@@ -97,10 +97,12 @@ function jsoncall($request, $service_uri = "") {
global $xbmcjsonservice;
global $DEBUG;
global $JSON_ERROR;
+ global $COMM_ERROR;
if($service_uri == "") {
$service_uri = $xbmcjsonservice;
}
+
//json rpc call procedure
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -109,17 +111,50 @@ function jsoncall($request, $service_uri = "") {
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
- $arrResult = json_decode($response, true);
-
curl_close($ch);
-
+
+ $arrResult = json_decode($response, true);
if((!empty($arrResult['error'])) && (!empty($DEBUG) && $DEBUG)) {
echo $JSON_ERROR;
+ echo "<p><strong>Last JSON Error: ".json_last_error()."</strong></p>\n";
+ echo "<p><strong>Request:</strong><pre>$request</pre></p>\n";
+ echo "<p><strong>Response:</strong><pre>$response</pre></p>\n";
+ }
+ if((empty($arrResult)) && (!empty($DEBUG) && $DEBUG)) {
+ echo $COMM_ERROR;
+ echo "<p><strong>".jsonerrorstring(json_last_error())."</strong></p>\n";
echo "<p><strong>Request:</strong><pre>$request</pre></p>\n";
echo "<p><strong>Response:</strong><pre>$response</pre></p>\n";
}
return $arrResult;
}
+function jsonerrorstring($err) {
+ switch($err) {
+ case JSON_ERROR_DEPTH:
+ $error = ' - Maximum stack depth exceeded';
+ break;
+ case JSON_ERROR_CTRL_CHAR:
+ $error = ' - Unexpected control character found';
+ break;
+ case JSON_ERROR_SYNTAX:
+ $error = ' - Syntax error, malformed JSON';
+ break;
+ case JSON_ERROR_STATE_MISMATCH:
+ $error = ' - Syntax error, Invalid or malformed JSON';
+ break;
+ //case JSON_ERROR_UTF8:
+ // $error = ' - Malformed UTF-8 characters, possibly incorrectly encoded';
+ // break;
+ case JSON_ERROR_NONE:
+ $error = '';
+ default:
+ $error = ' Error #'.$err;
+ }
+ if (!empty($error)) {
+ // throw new Exception('JSON Error: '.$error);
+ return "JSON Error: ".$error;
+ }
+}
?>
<?php
Please sign in to comment.
Something went wrong with that request. Please try again.