Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,36 @@ vendor/
.project
.settings
.buildpath


### Intellij
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
.idea/

### Numerous always-ignore extensions
*.diff
*.err
*.orig
*.ods
*.ods#
*.log
*.rej
*.swo
*.swp
*.zip
*.vi
*~

### OS or Editor folders
.DS_Store
._*
Thumbs.db
desktop.ini
.cache
.project
.settings
.tmproj
*.esproj
nbproject
*.sublime-project
*.sublime-workspace
81 changes: 64 additions & 17 deletions src/org/jsonrpcphp/JsonRPCClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @author sergio <[email protected]>
* @author Johannes Weberhofer <[email protected]>
*/

namespace org\jsonrpcphp;

class JsonRPCClient
Expand Down Expand Up @@ -87,7 +88,7 @@ public function __construct($url, $debug = false, $proxy = null)
{
$this->url = $url;
$this->proxy = $proxy;
$this->debug = ($this->debug === true);
$this->debug = $debug;
// message id
$this->id = 1;
}
Expand All @@ -108,13 +109,15 @@ public function setRPCNotification($notification)
*
* @param string $method
* @param array $params
*
* @return array
* @throws \Exception
*/
public function __call($method, $params)
{

// check
if (! is_scalar($method)) {
if ( ! is_scalar($method)) {
throw new \Exception('Method name has no scalar value');
}

Expand All @@ -137,11 +140,11 @@ public function __call($method, $params)
$request = array(
'method' => $method,
'params' => $params,
'id' => $currentId
'id' => $currentId,
);
$request = json_encode($request);
if ($this->debug) {
echo '***** Request *****' . "\n" . $request . "\n";
echo '***** Request *****'."\n".$request."\n";
}

// performs the HTTP POST
Expand All @@ -150,7 +153,7 @@ public function __call($method, $params)
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json'
'Content-type: application/json',
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
Expand All @@ -160,40 +163,84 @@ public function __call($method, $params)
}
$response = curl_exec($ch);
if ($response === false) {
throw new \Exception('Unable to connect to ' . $this->url);
throw new \Exception('Unable to connect to '.$this->url);
}
} else {
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $request
)
'content' => $request,
),
);
$context = stream_context_create($opts);

if ($fp = fopen($this->url, 'r', false, $context)) {
$response = '';
while ($row = fgets($fp)) {
$response .= trim($row) . "\n";
$response .= trim($row)."\n";
}
} else {
throw new \Exception('Unable to connect to ' . $this->url);
throw new \Exception('Unable to connect to '.$this->url);
}
}
if ($this->debug) {
echo '***** Response *****' . "\n" . $response . "\n" . '***** End of Response *****' . "\n\n";
echo '***** Response *****'."\n".$response."\n".'***** End of Response *****'."\n\n";
}
$response = json_decode($response, true);

// Check for errors
$jsonLastError = null;
switch (json_last_error()) {
case JSON_ERROR_NONE:
break;
case JSON_ERROR_DEPTH:
$jsonLastError = ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$jsonLastError = ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$jsonLastError = ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$jsonLastError = ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$jsonLastError = ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$jsonLastError = ' - Unknown error';
break;
}

// final checks and return
if (! $this->notification) {
// check
if ($response['id'] != $currentId) {
throw new \Exception('Incorrect response id: ' . $response['id'] . ' (request id: ' . $currentId . ')');
if ( ! $this->notification) {
$errorMessage = null;
switch (true) {
case ! is_array($response):
$errorMessage = sprintf('Incorrect response: %s. (jsonLastError: %s)', $response, $jsonLastError);
break;
case ! isset($response['id']):
$errorMessage = sprintf('The response has no response id! (jsonLastError: %s || response: %s)',
$jsonLastError, json_encode($response));
break;
case $response['id'] != $currentId:
$errorMessage = sprintf('Missmatching response id: recieved %s but was expecting %s . (response: %s)',
$response['id'], $currentId, json_encode($response));
break;
case array_key_exists('error', $response) && $response['error']:
$errorMessage = sprintf('Request error: %s', json_encode($response['error']));
break;
case ! array_key_exists('result', $response):
$errorMessage = sprintf('Request has no result to return! (response: %s)', json_encode($response));
break;

default:
}
if (array_key_exists('error', $response) && $response['error'] !== null) {
throw new \Exception('Request error: ' . json_encode($response['error']));

if ($errorMessage) {
throw new \Exception($errorMessage);
}

return $response['result'];
Expand Down