<p>As @Bittylicious mentioned the JSON-RPC is a little bit buggy</p>

<p><a href="https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#PHP" rel="nofollow">https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#PHP</a></p>

<p>Note: The jsonRPCClient library uses fopen() and will throw an exception saying "Unable to connect" if it receives a 404 or 500 error from bitcoind. This prevents you from being able to see error messages generated by bitcoind (as they are sent with status 404 or 500).</p>

<p>You have an option use <a href="https://github.com/aceat64/EasyBitcoin-PHP" rel="nofollow">EasyBitcoin</a> instead, that will throw more accurate error messages and its pretty much the same than JSON-RPC</p>

<pre><code>&lt;?php
 require_once 'YOUR_FOLDER/easybitcoin.php';
 $rpc_host = 'YOUR_IP';
 $rpc_port = 'YOUR_PORT';
 $rpc_user = 'YOUR_USER';
 $rpc_pass = 'YOUR_KEY';
 $bitcoin = new Bitcoin($rpc_user, $rpc_pass, $rpc_host, $rpc_port);
 print_r($bitcoin-&gt;getinfo());
 print_r($bitcoin-&gt;listaccounts());
 print_r($bitcoin-&gt;listreceivedbyaddress(0, true));
?&gt;
</code></pre>

<p>Hope this helps!</p>

CC BY-SA 3.0
2015-12-03T19:23:56.127Z
2015-12-04T23:11:12.777Z
2015-12-04T23:11:12.777Z
d1jhoni1b
<p>By default, bitcoind RPC goes over http, not https.
Unless of course you enabled SSL by generating an SSL key and setting the required config option...</p>

<pre><code> rpcssl=1
</code></pre>

<p>Otherwise, 
This line:</p>

<pre><code>$bitcoin = new jsonRPCClient('https://user:password@127.0.0.1:8332/');
</code></pre>

<p>Should be changed to:</p>

<pre><code>$bitcoin = new jsonRPCClient('http://user:password@127.0.0.1:8332/');
</code></pre>

<p>If that doesn't answer it let me know. There are a few other ways around it.</p>

<p>Welcome to the world of crystallographic currencies based on the satoshi client! </p>

<p>I think this little piece of info should be posted pretty inappropriately, it's hidden in documentation no one reads.</p>

CC BY-SA 3.0
2013-10-21T15:37:42.800Z
2014-01-05T12:21:02.837Z
2014-01-05T12:21:02.837Z
Joe White
<p>I resolved this problem by passing <code>-rpcthreads=16</code> even though the VPS server is a single core</p>

CC BY-SA 3.0
2017-08-12T15:02:39.177Z
2017-08-12T15:02:39.177Z
Kristopher Ives
<p>Just to add some useful information.</p>

<p>The jsonRPCClient does not return any useful information on errors, and all errors will just return the <em>could not connect</em> message. This is because bitcoind returns a HTTP error status code that's not 2xx. This is awful for debugging.</p>

<p>I have made a few hacky changes to the jsonRPCClient to get around this. What you need to do is:</p>

<ol>
<li>Add *'ignore_errors' => true* to the $opts array</li>
<li>Change the error line that throws <em>Request error</em> to read <em>throw new Exception('Request error: '.$response['error']['code'].' - '.$response['error']['message']);</em>.</li>
</ol>

<p>The latter change inparticular makes the jsonRPCClient specific to bitcoind, but it shouldn't be assuming that error is a string anyway.</p>

CC BY-SA 3.0
2013-11-06T09:22:18.197Z
2013-11-06T09:22:18.197Z
Bittylicious
<p>Testnet connects on port 18332 unless you specify set the port in your conf file. Changing it to that should correct the issue.</p>

CC BY-SA 3.0
2014-01-05T17:41:36.610Z
2014-01-05T17:41:36.610Z
michael.schuett
<p>I'm able to connect to bitcoind using my command line and the process is running with the appropriate port but when I try to run it through XAMPP after using the JSON-RPC library (I used the PHP developer intro for the code) it's giving me the following error, and I don't know why: </p>

<pre><code>Warning:  fopen(): Failed to enable crypto in /opt/lampp/htdocs/bitcoins/jsonRPCClient.php on line 132

Warning:  fopen(https://...@127.0.0.1:8332/): failed to open stream: operation failed in /opt/lampp/htdocs/bitcoins/jsonRPCClient.php on line 132
Fatal error:  Uncaught exception 'Exception' with message 'Unable to connect to https://user:password
@127.0.0.1:8332/' in /opt/lampp/htdocs/bitcoins/jsonRPCClient.php:140
Stack trace:
#0 /opt/lampp/htdocs/bitcoins/testServer.php(7): jsonRPCClient-&gt;__call('getinfo', Array)
#1 /opt/lampp/htdocs/bitcoins/testServer.php(7): jsonRPCClient-&gt;getinfo()
#2 {main}
  thrown in /opt/lampp/htdocs/bitcoins/jsonRPCClient.php on line 140
</code></pre>

<p>The code is the following: (we tried toggling between http and https and it's still not working) </p>

<pre><code>require_once 'jsonRPCClient.php';

$bitcoin = new jsonRPCClient('https://user:password@127.0.0.1:8332/');

echo "&lt;pre&gt;\n";
print_r($bitcoin-&gt;getinfo());
echo "&lt;/pre&gt;";
</code></pre>

CC BY-SA 3.0
2013-10-20T22:10:34.007Z
are you sure you are providing the right login/password?
Salvador Dali
CC BY-SA 3.0
2013-10-21T02:57:07.617Z
@SalvadorDali: I copied and pasted it from bitcoin.conf, also tried shifting the hosting to Amazon EC2 and it totally didn't work.
Vinamrata
<bitcoind><json-rpc><server><php>
JSON RPC library unable to connect to bitcoind?
Vinamrata
# JSON RPC library unable to connect to bitcoind?

(Asked by: username_0 on 2013-10-20)

I'm able to connect to bitcoind using my command line and the process is running with the appropriate port but when I try to run it through XAMPP after using the JSON-RPC library (I used the PHP developer intro for the code) it's giving me the following error, and I don't know why:

```
Warning:  fopen(): Failed to enable crypto in /opt/lampp/htdocs/bitcoins/jsonRPCClient.php on line 132

Warning:  fopen(https://...@127.0.0.1:8332/): failed to open stream: operation failed in /opt/lampp/htdocs/bitcoins/jsonRPCClient.php on line 132
Fatal error:  Uncaught exception 'Exception' with message 'Unable to connect to https://user:password
@127.0.0.1:8332/' in /opt/lampp/htdocs/bitcoins/jsonRPCClient.php:140
Stack trace:
#0 /opt/lampp/htdocs/bitcoins/testServer.php(7): jsonRPCClient->__call('getinfo', Array)
#1 /opt/lampp/htdocs/bitcoins/testServer.php(7): jsonRPCClient->getinfo()
#2 {main}
  thrown in /opt/lampp/htdocs/bitcoins/jsonRPCClient.php on line 140

```

The code is the following: (we tried toggling between http and https and it's still not working)

```
require_once 'jsonRPCClient.php';

$bitcoin = new jsonRPCClient('https://user:password@127.0.0.1:8332/');

echo "<pre>\n";
print_r($bitcoin->getinfo());
echo "</pre>";

```

**Tags:** bitcoind, json-rpc, server, php

> **Comments:**
> **username_1**: are you sure you are providing the right login/password?  
> **username_0**: @username_1: I copied and pasted it from bitcoin.conf, also tried shifting the hosting to Amazon EC2 and it totally didn't work.  

---

## 5 answers

### Answer by username_2 on 2015-12-03. Score: 2

As @username_5 mentioned the JSON-RPC is a little bit buggy

<https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#PHP>

Note: The jsonRPCClient library uses fopen() and will throw an exception saying "Unable to connect" if it receives a 404 or 500 error from bitcoind. This prevents you from being able to see error messages generated by bitcoind (as they are sent with status 404 or 500).

You have an option use [EasyBitcoin](https://github.com/aceat64/EasyBitcoin-PHP) instead, that will throw more accurate error messages and its pretty much the same than JSON-RPC

```
<?php
 require_once 'YOUR_FOLDER/easybitcoin.php';
 $rpc_host = 'YOUR_IP';
 $rpc_port = 'YOUR_PORT';
 $rpc_user = 'YOUR_USER';
 $rpc_pass = 'YOUR_KEY';
 $bitcoin = new Bitcoin($rpc_user, $rpc_pass, $rpc_host, $rpc_port);
 print_r($bitcoin->getinfo());
 print_r($bitcoin->listaccounts());
 print_r($bitcoin->listreceivedbyaddress(0, true));
?>

```

Hope this helps!


---

### Answer by username_3 on 2013-10-21. Score: 1

By default, bitcoind RPC goes over http, not https.
Unless of course you enabled SSL by generating an SSL key and setting the required config option...

```
 rpcssl=1

```

Otherwise,
This line:

```
$bitcoin = new jsonRPCClient('https://user:password@127.0.0.1:8332/');

```

Should be changed to:

```
$bitcoin = new jsonRPCClient('http://user:password@127.0.0.1:8332/');

```

If that doesn't answer it let me know. There are a few other ways around it.

Welcome to the world of crystallographic currencies based on the satoshi client!

I think this little piece of info should be posted pretty inappropriately, it's hidden in documentation no one reads.


---

### Answer by username_4 on 2017-08-12. Score: 1

I resolved this problem by passing `-rpcthreads=16` even though the VPS server is a single core


---

### Answer by username_5 on 2013-11-06. Score: 0

Just to add some useful information.

The jsonRPCClient does not return any useful information on errors, and all errors will just return the *could not connect* message. This is because bitcoind returns a HTTP error status code that's not 2xx. This is awful for debugging.

I have made a few hacky changes to the jsonRPCClient to get around this. What you need to do is:

1. Add \*'ignore\_errors' => true\* to the $opts array
2. Change the error line that throws *Request error* to read *throw new Exception('Request error: '.$response['error']['code'].' - '.$response['error']['message']);*.

The latter change inparticular makes the jsonRPCClient specific to bitcoind, but it shouldn't be assuming that error is a string anyway.


---

### Answer by username_6 on 2014-01-05. Score: 0

Testnet connects on port 18332 unless you specify set the port in your conf file. Changing it to that should correct the issue.


---

