Bitcoin Forum
May 23, 2024, 07:44:44 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: PHP and bitcoinQT error when connecting via jsonRPC  (Read 1432 times)
spunkybd (OP)
Full Member
***
Offline Offline

Activity: 260
Merit: 102



View Profile
April 21, 2013, 04:02:32 AM
 #1

I have set up my .conf file for bitcoin and a php script to connect to the RPC server. Whenever i try to connect through my web browser i get this error:

{"result":null,"error":{"code":-32700,"message":"Parse error"},"id":null}

Any tips?
OdinHephaestus
Member
**
Offline Offline

Activity: 70
Merit: 10



View Profile
April 21, 2013, 04:14:21 AM
 #2

The daemon had trouble parsing the json you sent it.

Without a code snippet, I can't help you any more than that.

Bitcoin: 1odinQ5YUdbfuopNF2D1FZRN8qcrC4y1L
Litecoin: LeodinXzG7CgHqPDZxaUgsDKUKBUu46qa5
spunkybd (OP)
Full Member
***
Offline Offline

Activity: 260
Merit: 102



View Profile
April 21, 2013, 04:29:38 AM
 #3

The daemon had trouble parsing the json you sent it.

Without a code snippet, I can't help you any more than that.

Code:
/* Configuration variables for the JSON-RPC server */
$rpc_host = 'localhost';
$rpc_port = '8332';
$rpc_user = 'thisuser';
$rpc_pass = 'mypass';


require_once('jsonRPCClient.php');
$bc = new jsonRPCClient('http://' . $rpc_user . ':' . $rpc_pass . '@' . $rpc_host . ':' . $rpc_port);

Also conf:

Code:
# JSON-RPC options (for controlling a running Bitcoin/bitcoind process)
 
 # server=1 tells Bitcoin-QT to accept JSON-RPC commands.
 server=1
 
 # You must set rpcuser and rpcpassword to secure the JSON-RPC api
 rpcuser=thisuser
 rpcpassword=thispass
 
 # How many seconds bitcoin will wait for a complete RPC HTTP request.
 # after the HTTP connection is established.
 rpctimeout=30
 
 # By default, only RPC connections from localhost are allowed.  Specify
 # as many rpcallowip= settings as you like to allow connections from
 # other hosts (and you may use * as a wildcard character):
 #rpcallowip=10.1.1.34
 rpcallowip=127.0.0.1
 rpcallowip=10.1.10.100
 rpcallowip=localhost
 
 # Listen for RPC connections on this TCP port:
 rpcport=8332
 
OdinHephaestus
Member
**
Offline Offline

Activity: 70
Merit: 10



View Profile
April 21, 2013, 04:43:33 AM
 #4

I'm assuming your username's and passes's are matching.

What function are you calling to get the error?

Bitcoin: 1odinQ5YUdbfuopNF2D1FZRN8qcrC4y1L
Litecoin: LeodinXzG7CgHqPDZxaUgsDKUKBUu46qa5
spunkybd (OP)
Full Member
***
Offline Offline

Activity: 260
Merit: 102



View Profile
April 21, 2013, 04:47:18 AM
 #5

I'm assuming your username's and passes's are matching.

What function are you calling to get the error?




This here is supposed to grab the info from up there. It does so properly and tries to connect but i receive the error i posted up top.


require_once('jsonRPCClient.php');
$bc = new jsonRPCClient('http://' . $rpc_user . ':' . $rpc_pass . '@' . $rpc_host . ':' . $rpc_port);
OdinHephaestus
Member
**
Offline Offline

Activity: 70
Merit: 10



View Profile
April 21, 2013, 04:54:42 AM
 #6

Why do you have...

Code:
rpcallowip=127.0.0.1
rpcallowip=localhost

These are the same thing, I would remove the "localhost" one, it is better to use the ip 127.0.0.1 for localhost. Not a hostname.


Way to nitpick.

As for your problem, change
Code:
$bc = new jsonRPCClient('http://' . $rpc_user . ':' . $rpc_pass . '@' . $rpc_host . ':' . $rpc_port);
to
Code:
$bc = new jsonRPCClient('http://' . $rpc_user . ':' . $rpc_pass . '@' . $rpc_host . ':' . $rpc_port, true);

This should give you a more in depth error log.

Barring that, try re-downloading the json-rpc library, it's possible yours might have been messed with. Were you playing with the code at all?

Bitcoin: 1odinQ5YUdbfuopNF2D1FZRN8qcrC4y1L
Litecoin: LeodinXzG7CgHqPDZxaUgsDKUKBUu46qa5
spunkybd (OP)
Full Member
***
Offline Offline

Activity: 260
Merit: 102



View Profile
April 21, 2013, 04:58:23 AM
 #7

Ill try what you suggested then report back and then replace the json client.

EDIT:

Didnt work with the replaced RPC client or with the true statement

RPCclient im using:

Code:
/**
 * The object of this class are generic jsonRPC 1.0 clients
 * http://json-rpc.org/wiki/specification
 *
 * @author sergio <jsonrpcphp@inservibile.org>
 */
class jsonRPCClient {

/**
* Debug state
*
* @var boolean
*/
private $debug;

/**
* The server URL
*
* @var string
*/
private $url;
/**
* The request id
*
* @var integer
*/
private $id;
/**
* If true, notifications are performed instead of requests
*
* @var boolean
*/
private $notification = false;

/**
* Takes the connection parameters
*
* @param string $url
* @param boolean $debug
*/
public function __construct($url,$user_pwd,$debug = false) {
// server URL
$this->url = $url;
// user:password
$this->user_pwd = $user_pwd;
// proxy
empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
// debug state
empty($debug) ? $this->debug = false : $this->debug = true;
// message id
$this->id = 1;
}

/**
* Sets the notification state of the object. In this state, notifications are performed, instead of requests.
*
* @param boolean $notification
*/
public function setRPCNotification($notification) {
empty($notification) ?
$this->notification = false
:
$this->notification = true;
}

/**
* Performs a jsonRCP request and gets the results as an array
*
* @param string $method
* @param array $params
* @return array
*/
public function __call($method,$params) {

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

// check
if (is_array($params)) {
// no keys
$params = array_values($params);
} else {
throw new Exception('Params must be given as array');
}

// sets notification or request task
if ($this->notification) {
$currentId = NULL;
} else {
$currentId = $this->id;
}

// prepares the request
$request = array(
'method' => $method,
'params' => $params,
'id' => $currentId
);
$request = json_encode($request);
$this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";

// performs the HTTP POST
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $this->url,
            CURLOPT_USERPWD => $this->user_pwd,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $request,
            CURLOPT_HTTPHEADER => array('Content-type: application/json'),
            CURLOPT_RETURNTRANSFER => true,
        ));
       
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($http_code < 200 || $http_code >= 300)
        {
throw new Exception('Unable to connect to '.$this->url);
}
       
        $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
        $response = json_decode($response,true);

// debug output
if ($this->debug) {
echo nl2br($debug);
}

// final checks and return
if (!$this->notification) {
// check
if ($response['id'] != $currentId) {
// throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.print_r($response,true).')');
}
if (!is_null($response['error'])) {
// throw new Exception('Request error: '.$response['error']['message']);
}

return $response['result'];

} else {
return true;
}
}
}
?>
spunkybd (OP)
Full Member
***
Offline Offline

Activity: 260
Merit: 102



View Profile
April 21, 2013, 05:11:09 AM
 #8

Why do you have...

Code:
rpcallowip=127.0.0.1
rpcallowip=localhost

These are the same thing, I would remove the "localhost" one, it is better to use the ip 127.0.0.1 for localhost. Not a hostname.

Meh best practice where i come from is to use the hostname Wink
OdinHephaestus
Member
**
Offline Offline

Activity: 70
Merit: 10



View Profile
April 21, 2013, 05:25:20 AM
 #9

Do you have a specific preference for cURL?

jsonRPCClient.php
Code:
<?php
/**
 * The object of this class are generic jsonRPC 1.0 clients
 * http://json-rpc.org/wiki/specification
 *
 * @author sergio <jsonrpcphp@inservibile.org>
 */
class jsonRPCClient {

/**
 * Debug state
 *
 * @var boolean
 */
private $debug;

/**
 * The server URL
 *
 * @var string
 */
private $url;
/**
 * The request id
 *
 * @var integer
 */
private $id;
/**
 * If true, notifications are performed instead of requests
 *
 * @var boolean
 */
private $notification false;

/**
 * Takes the connection parameters
 *
 * @param string $url
 * @param boolean $debug
 */
public function __construct($url,$debug false) {
// server URL
$this->url $url;
// proxy
empty($proxy) ? $this->proxy '' $this->proxy $proxy;
// debug state
empty($debug) ? $this->debug false $this->debug true;
// message id
$this->id 1;
}

/**
 * Sets the notification state of the object. In this state, notifications are performed, instead of requests.
 *
 * @param boolean $notification
 */
public function setRPCNotification($notification) {
empty($notification) ?
$this->notification false
:
$this->notification true;
}

/**
 * Performs a jsonRCP request and gets the results as an array
 *
 * @param string $method
 * @param array $params
 * @return array
 */
public function __call($method,$params) {

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

// check
if (is_array($params)) {
// no keys
$params array_values($params);
} else {
throw new Exception('Params must be given as array');
}

// sets notification or request task
if ($this->notification) {
$currentId NULL;
} else {
$currentId $this->id;
}

// prepares the request
$request = array(
'method' => $method,
'params' => $params,
'id' => $currentId
);
$request json_encode($request);
$this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";

// performs the HTTP POST
$opts = array ('http' => array (
'method'  => 'POST',
'header'  => 'Content-type: application/json',
'content' => $request
));
$context  stream_context_create($opts);
if ($fp fopen($this->url'r'false$context)) {
$response '';
while($row fgets($fp)) {
$response.= trim($row)."\n";
}
$this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
$response json_decode($response,true);
} else {
throw new Exception('Unable to connect to '.$this->url);
}

// debug output
if ($this->debug) {
echo nl2br($debug);
}

// final checks and return
if (!$this->notification) {
// check
if ($response['id'] != $currentId) {
throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
}
if (!is_null($response['error'])) {
throw new Exception('Request error: '.$response['error']);
}

return $response['result'];

} else {
return true;
}
}
}
?>

Bitcoin: 1odinQ5YUdbfuopNF2D1FZRN8qcrC4y1L
Litecoin: LeodinXzG7CgHqPDZxaUgsDKUKBUu46qa5
spunkybd (OP)
Full Member
***
Offline Offline

Activity: 260
Merit: 102



View Profile
April 21, 2013, 05:37:01 AM
 #10


That should be removed

You only need the password in the URL not in the curl script.


Your a genius and i love you.

Thanks everyone for your help, its really appreciated. And when i become BTC rich i will think of you for a second.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!