3

When I execute my php file from the command line I get this:

<pre>
PHP Warning:  fopen(http://...@127.0.0.1:8332/): failed to open stream: HTTP request failed! HTTP/1.0 401 Authorization Required
 in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php on line 132
PHP Fatal error:  Uncaught exception 'Exception' with message 'Unable to connect to http://b:a@127.0.0.1:8332/' in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php:140
Stack trace:
#0 /home/brian/Desktop/VH2_apache2/host02/btc16/index1.php(12): jsonRPCClient->__call('getinfo', Array)
#1 /home/brian/Desktop/VH2_apache2/host02/btc16/index1.php(12): jsonRPCClient->getinfo()
#2 {main}
  thrown in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php on line 140

My php file looks like this:

<?php
  require_once 'jsonRPCClient.php';

  $bitcoin = new jsonRPCClient('http://user:password@127.0.0.1:8332/',true);
  #$param = array(
  #'c418518d5ffccd947e377ab7e589f684d83e6df1af8f397cc11848ac8a78f0dd',
  #'1');

  #$return1 = $bitcoin->getrawtransaction('c418518d5ffccd947e377ab7e589f684d83e6df1af8f397cc11848ac8a78f0dd','1')

  echo "<pre>\n";
  print_r($bitcoin->getinfo()); echo "\n";
  #print_r($return1['0']); echo "\n";
  #echo "Received: ".$bitcoin->getreceivedbylabel("Your Address")."\n";
  echo "</pre>";
?>

My webpage with apache2 appears blank that uses the php file I made. I am using the -txindex parameter when I launch bitcoind.

But I am able to use bitcoin-cli to interact with bitcoind

Iv'e used:
bitcoin-cli getrawtransaction c418518d5ffccd947e377ab7e589f684d83e6df1af8f397cc11848ac8a78f0dd 1

and:
bitcoin-cli getinfo

Also my bitcoin.conf looks like this:

rpcuser=user
rpcpassword=pass
txindex=1
server=1

Also note that I substituted the usernames and passwords to hide them.

My question is why can I use bitcoin-cli and get no error, but when I try to use the jsonRPCClient.php I get the error 401 Authorization Required and how do I fix the error?

  • Error 401 means bad user/password. I notice that the password is pass in your config file, and it's password in your config file. Does that reflect the contents of your config/php files? – Nick ODell Apr 17 '15 at 18:28
  • No, sorry for the confusion, I replaced my password and username listed on this forum to maintain security of the bitcoin core on my computer. The password and username I used on my computer match with those in the config file and I have checked many times. – Gemohn Herbile Apr 17 '15 at 23:48
  • I dunno what to tell you. Bad user/pass is what triggers the error. – Nick ODell Apr 17 '15 at 23:59
  • Have you restarted your client since putting the username and password in the conf file? – morsecoder Apr 18 '15 at 13:12
  • I think so, I have re-started my php script and bitcoind as well, and the wierd thing is that it worked at one point. – Gemohn Herbile Apr 18 '15 at 13:26
0

I think this is related to this open issue.

https://github.com/bitcoin/bitcoin/issues/9661

I was running bitcoind on Ubuntu 16.

I had the similar issue when I had rpcuser and rpcpassword in the config file. All I got is 401 error when I called the jaon-rpc api.

I found a workaround by providing -rpcuser=REPLACED -rpcpassword=REPLACED when I launched the bitcoind.

Hope this is useful.

One more thing, instead of debug from your rpc client in PHP. Maybe try it with curl first. HTTP 401 clearly is an authentication issue, using curl is much easier for debugging.

0

Please make sure that your RPC username and password are correct, also make sure that your username and password don't contain (#) tag.

0

Make sure to check which bitcoin.conf configuration file the bitcoind daemon is using. Its explicitly shown on bootstrapping.

This would arise if you change default bitcoin-core installation path on setup.

This way you won't need to pass the user/pwd via cli on service start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.