<p><code>$bitcoin = new Bitcoin("rpcuser","rpcpassword","localhost","80");</code></p>

<p>Port 80 is HTTP - it should be 8332 (or 18332 for testnet) unless you have set another RPC port in the configuration file</p>

CC BY-SA 3.0
2017-01-27T21:50:21.810Z
I tried several ports : 8332 8333 80
smoumou
CC BY-SA 3.0
2017-01-27T10:18:54.363Z
2017-01-27T10:18:54.363Z
Alex
<p>Bitcoin RPC is working on UBUNTU but not on Windows. </p>

<p>I am using exactly the same library.</p>

<p>My php code : </p>

<pre><code>&lt;?php
require_once('../Easy_btc/easybitcoin.php');
$bitcoin = new Bitcoin("rpcuser","rpcpassword","localhost","80");
$b = $bitcoin-&gt;getinfo();
var_dump($b);
var_dump($bitcoin);
?&gt;
</code></pre>

<p>The error I get : </p>

<pre><code>object(Bitcoin)#1 (12) {
  ["username":"Bitcoin":private]=&gt;
  string(7) "rpcuser"
  ["password":"Bitcoin":private]=&gt;
  string(11) "rpcpassword"
  ["proto":"Bitcoin":private]=&gt;
  string(4) "http"
  ["host":"Bitcoin":private]=&gt;
  string(9) "localhost"
  ["port":"Bitcoin":private]=&gt;
  string(2) "80"
  ["url":"Bitcoin":private]=&gt;
  NULL
  ["CACertificate":"Bitcoin":private]=&gt;
  NULL
  ["status"]=&gt;
  int(0)
  ["error"]=&gt;
  string(23) "Empty reply from server"
  ["raw_response"]=&gt;
  bool(false)
  ["response"]=&gt;
  NULL
  ["id":"Bitcoin":private]=&gt;
  int(1)
}
</code></pre>

<p>The library I use : </p>

<pre><code>&lt;?php
/*
EasyBitcoin-PHP

A simple class for making calls to Bitcoin's API using PHP.
https://github.com/aceat64/EasyBitcoin-PHP

====================

The MIT License (MIT)

Copyright (c) 2013 Andrew LeCody

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

====================

// Initialize Bitcoin connection/object
$bitcoin = new Bitcoin('username','password');

// Optionally, you can specify a host and port.
$bitcoin = new Bitcoin('username','password','host','port');
// Defaults are:
//  host = localhost
//  port = 8332
//  proto = http

// If you wish to make an SSL connection you can set an optional CA certificate or leave blank
// This will set the protocol to HTTPS and some CURL flags
$bitcoin-&gt;setSSL('/full/path/to/mycertificate.cert');

// Make calls to bitcoind as methods for your object. Responses are returned as an array.
// Examples:
$bitcoin-&gt;getinfo();
$bitcoin-&gt;getrawtransaction('0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098',1);
$bitcoin-&gt;getblock('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');

// The full response (not usually needed) is stored in $this-&gt;response while the raw JSON is stored in $this-&gt;raw_response

// When a call fails for any reason, it will return FALSE and put the error message in $this-&gt;error
// Example:
echo $bitcoin-&gt;error;

// The HTTP status code can be found in $this-&gt;status and will either be a valid HTTP status code or will be 0 if cURL was unable to connect.
// Example:
echo $bitcoin-&gt;status;

*/

class Bitcoin {
    // Configuration options
    private $username;
    private $password;
    private $proto;
    private $host;
    private $port;
    private $url;
    private $CACertificate;

    // Information and debugging
    public $status;
    public $error;
    public $raw_response;
    public $response;

    private $id = 0;

    /**
     * @param string $username
     * @param string $password
     * @param string $host
     * @param int $port
     * @param string $proto
     * @param string $url
     */
    function __construct($username, $password, $host = 'localhost', $port = 8332, $url = null) {
        $this-&gt;username      = $username;
        $this-&gt;password      = $password;
        $this-&gt;host          = $host;
        $this-&gt;port          = $port;
        $this-&gt;url           = $url;

        // Set some defaults
        $this-&gt;proto         = 'http';
        $this-&gt;CACertificate = null;
    }

    /**
     * @param string|null $certificate
     */
    function setSSL($certificate = null) {
        $this-&gt;proto         = 'https'; // force HTTPS
        $this-&gt;CACertificate = $certificate;
    }

    function __call($method, $params) {
        $this-&gt;status       = null;
        $this-&gt;error        = null;
        $this-&gt;raw_response = null;
        $this-&gt;response     = null;

        // If no parameters are passed, this will be an empty array
        $params = array_values($params);

        // The ID should be unique for each call
        $this-&gt;id++;

        // Build the request, it's ok that params might have any empty array
        $request = json_encode(array(
            'method' =&gt; $method,
            'params' =&gt; $params,
            'id'     =&gt; $this-&gt;id
        ));

        // Build the cURL session
        //$curl    = curl_init("{$this-&gt;proto}://{$this-&gt;host}:{$this-&gt;port}/{$this-&gt;url}");
        $curl    = curl_init("{$this-&gt;proto}://{$this-&gt;username}:{$this-&gt;password}@{$this-&gt;host}:{$this-&gt;port}/{$this-&gt;url}");
        $options = array(
            //CURLOPT_HTTPAUTH       =&gt; CURLAUTH_BASIC,
            //CURLOPT_HTTPAUTH       =&gt; TRUE,
            //CURLOPT_USERPWD        =&gt; $this-&gt;username . ':' . $this-&gt;password,
            CURLOPT_RETURNTRANSFER =&gt; TRUE,
            CURLOPT_FOLLOWLOCATION =&gt; TRUE,
            CURLOPT_MAXREDIRS      =&gt; 10,
            CURLOPT_HTTPHEADER     =&gt; array('Content-type: application/json'),
            CURLOPT_POST           =&gt; TRUE,
            CURLOPT_POSTFIELDS     =&gt; $request
        );

        // This prevents users from getting the following warning when open_basedir is set:
        // Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set
        if (ini_get('open_basedir')) {
            unset($options[CURLOPT_FOLLOWLOCATION]);
        }

        if ($this-&gt;proto == 'https') {
            // If the CA Certificate was specified we change CURL to look for it
            if ($this-&gt;CACertificate != null) {
                $options[CURLOPT_CAINFO] = $this-&gt;CACertificate;
                $options[CURLOPT_CAPATH] = DIRNAME($this-&gt;CACertificate);
            }
            else {
                // If not we need to assume the SSL cannot be verified so we set this flag to FALSE to allow the connection
                $options[CURLOPT_SSL_VERIFYPEER] = FALSE;
            }
        }

        curl_setopt_array($curl, $options);

        // Execute the request and decode to an array
        $this-&gt;raw_response = curl_exec($curl);
        $this-&gt;response     = json_decode($this-&gt;raw_response, TRUE);

        // If the status is not 200, something is wrong
        $this-&gt;status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        // If there was no error, this will be an empty string
        $curl_error = curl_error($curl);

        curl_close($curl);

        if (!empty($curl_error)) {
            $this-&gt;error = $curl_error;
        }

        if ($this-&gt;response['error']) {
            // If bitcoind returned an error, put that in $this-&gt;error
            $this-&gt;error = $this-&gt;response['error']['message'];
        }
        elseif ($this-&gt;status != 200) {
            // If bitcoind didn't return a nice error message, we need to make our own
            switch ($this-&gt;status) {
                case 400:
                    $this-&gt;error = 'HTTP_BAD_REQUEST';
                    break;
                case 401:
                    $this-&gt;error = 'HTTP_UNAUTHORIZED';
                    break;
                case 403:
                    $this-&gt;error = 'HTTP_FORBIDDEN';
                    break;
                case 404:
                    $this-&gt;error = 'HTTP_NOT_FOUND';
                    break;
            }
        }

        if ($this-&gt;error) {
            return FALSE;
        }

        return $this-&gt;response['result'];
    }
}
?&gt;
</code></pre>

<p>Thanks in advance.</p>

CC BY-SA 3.0
2017-01-24T20:01:36.177Z
Is Bitcoin running in RPC mode on both computers? What port do you have Bitcoin configured to use for RPC?
Nick ODell
CC BY-SA 3.0
2017-01-24T20:11:28.110Z
I resolved it using bitcoind commands -server -rpcuser -rpcpassword
smoumou
CC BY-SA 3.0
2017-01-24T20:12:47.303Z
don't know why on windows either with bitcoin-core or bitoind I cannot update and set the bitcoin.conf file. It is always ignored
smoumou
CC BY-SA 3.0
2017-01-24T20:15:25.977Z
Are you putting the bitcoin.conf file in the right place? Are you restarting Bitcoin between attempts to configure it? Can you use -conf at command line to manually set the path to the configuration file?
Nick ODell
CC BY-SA 3.0
2017-01-24T20:18:11.283Z
I already did all of that hundreds of times ...
smoumou
CC BY-SA 3.0
2017-08-25T08:02:58.417Z
@smoumou Do you literally mean you added "-rpcuser" and "-rpcpassword" by themselves as command line options?  Or do you mean that you put the actual username and password?  Or the options followed by the username and password?  I searched and don't see any documentation on those command line options so not sure how I should implement them to fix the same 401 problem.
stone.212
<client><json-rpc>
I can not access use bitcoin rpc via php : easybitcoin.php
smoumou
# I can not access use bitcoin rpc via php : easybitcoin.php

(Asked by: username_0 on 2017-01-24)

Bitcoin RPC is working on UBUNTU but not on Windows.

I am using exactly the same library.

My php code :

```
<?php
require_once('../Easy_btc/easybitcoin.php');
$bitcoin = new Bitcoin("rpcuser","rpcpassword","localhost","80");
$b = $bitcoin->getinfo();
var_dump($b);
var_dump($bitcoin);
?>

```

The error I get :

```
object(Bitcoin)#1 (12) {
  ["username":"Bitcoin":private]=>
  string(7) "rpcuser"
  ["password":"Bitcoin":private]=>
  string(11) "rpcpassword"
  ["proto":"Bitcoin":private]=>
  string(4) "http"
  ["host":"Bitcoin":private]=>
  string(9) "localhost"
  ["port":"Bitcoin":private]=>
  string(2) "80"
  ["url":"Bitcoin":private]=>
  NULL
  ["CACertificate":"Bitcoin":private]=>
  NULL
  ["status"]=>
  int(0)
  ["error"]=>
  string(23) "Empty reply from server"
  ["raw_response"]=>
  bool(false)
  ["response"]=>
  NULL
  ["id":"Bitcoin":private]=>
  int(1)
}

```

The library I use :

```
<?php
/*
EasyBitcoin-PHP

A simple class for making calls to Bitcoin's API using PHP.
https://github.com/aceat64/EasyBitcoin-PHP

====================

The MIT License (MIT)

Copyright (c) 2013 Andrew LeCody

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

====================

// Initialize Bitcoin connection/object
$bitcoin = new Bitcoin('username','password');

// Optionally, you can specify a host and port.
$bitcoin = new Bitcoin('username','password','host','port');
// Defaults are:
//  host = localhost
//  port = 8332
//  proto = http

// If you wish to make an SSL connection you can set an optional CA certificate or leave blank
// This will set the protocol to HTTPS and some CURL flags
$bitcoin->setSSL('/full/path/to/mycertificate.cert');

// Make calls to bitcoind as methods for your object. Responses are returned as an array.
// Examples:
$bitcoin->getinfo();
$bitcoin->getrawtransaction('0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098',1);
$bitcoin->getblock('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');

// The full response (not usually needed) is stored in $this->response while the raw JSON is stored in $this->raw_response

// When a call fails for any reason, it will return FALSE and put the error message in $this->error
// Example:
echo $bitcoin->error;

// The HTTP status code can be found in $this->status and will either be a valid HTTP status code or will be 0 if cURL was unable to connect.
// Example:
echo $bitcoin->status;

*/

class Bitcoin {
    // Configuration options
    private $username;
    private $password;
    private $proto;
    private $host;
    private $port;
    private $url;
    private $CACertificate;

    // Information and debugging
    public $status;
    public $error;
    public $raw_response;
    public $response;

    private $id = 0;

    /**
     * @param string $username
     * @param string $password
     * @param string $host
     * @param int $port
     * @param string $proto
     * @param string $url
     */
    function __construct($username, $password, $host = 'localhost', $port = 8332, $url = null) {
        $this->username      = $username;
        $this->password      = $password;
        $this->host          = $host;
        $this->port          = $port;
        $this->url           = $url;

        // Set some defaults
        $this->proto         = 'http';
        $this->CACertificate = null;
    }

    /**
     * @param string|null $certificate
     */
    function setSSL($certificate = null) {
        $this->proto         = 'https'; // force HTTPS
        $this->CACertificate = $certificate;
    }

    function __call($method, $params) {
        $this->status       = null;
        $this->error        = null;
        $this->raw_response = null;
        $this->response     = null;

        // If no parameters are passed, this will be an empty array
        $params = array_values($params);

        // The ID should be unique for each call
        $this->id++;

        // Build the request, it's ok that params might have any empty array
        $request = json_encode(array(
            'method' => $method,
            'params' => $params,
            'id'     => $this->id
        ));

        // Build the cURL session
        //$curl    = curl_init("{$this->proto}://{$this->host}:{$this->port}/{$this->url}");
        $curl    = curl_init("{$this->proto}://{$this->username}:{$this->password}@{$this->host}:{$this->port}/{$this->url}");
        $options = array(
            //CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,
            //CURLOPT_HTTPAUTH       => TRUE,
            //CURLOPT_USERPWD        => $this->username . ':' . $this->password,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_FOLLOWLOCATION => TRUE,
            CURLOPT_MAXREDIRS      => 10,
            CURLOPT_HTTPHEADER     => array('Content-type: application/json'),
            CURLOPT_POST           => TRUE,
            CURLOPT_POSTFIELDS     => $request
        );

        // This prevents users from getting the following warning when open_basedir is set:
        // Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set
        if (ini_get('open_basedir')) {
            unset($options[CURLOPT_FOLLOWLOCATION]);
        }

        if ($this->proto == 'https') {
            // If the CA Certificate was specified we change CURL to look for it
            if ($this->CACertificate != null) {
                $options[CURLOPT_CAINFO] = $this->CACertificate;
                $options[CURLOPT_CAPATH] = DIRNAME($this->CACertificate);
            }
            else {
                // If not we need to assume the SSL cannot be verified so we set this flag to FALSE to allow the connection
                $options[CURLOPT_SSL_VERIFYPEER] = FALSE;
            }
        }

        curl_setopt_array($curl, $options);

        // Execute the request and decode to an array
        $this->raw_response = curl_exec($curl);
        $this->response     = json_decode($this->raw_response, TRUE);

        // If the status is not 200, something is wrong
        $this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        // If there was no error, this will be an empty string
        $curl_error = curl_error($curl);

        curl_close($curl);

        if (!empty($curl_error)) {
            $this->error = $curl_error;
        }

        if ($this->response['error']) {
            // If bitcoind returned an error, put that in $this->error
            $this->error = $this->response['error']['message'];
        }
        elseif ($this->status != 200) {
            // If bitcoind didn't return a nice error message, we need to make our own
            switch ($this->status) {
                case 400:
                    $this->error = 'HTTP_BAD_REQUEST';
                    break;
                case 401:
                    $this->error = 'HTTP_UNAUTHORIZED';
                    break;
                case 403:
                    $this->error = 'HTTP_FORBIDDEN';
                    break;
                case 404:
                    $this->error = 'HTTP_NOT_FOUND';
                    break;
            }
        }

        if ($this->error) {
            return FALSE;
        }

        return $this->response['result'];
    }
}
?>

```

Thanks in advance.

**Tags:** client, json-rpc

> **Comments:**
> **username_1**: Is Bitcoin running in RPC mode on both computers? What port do you have Bitcoin configured to use for RPC?  
> **username_0**: I resolved it using bitcoind commands -server -rpcuser -rpcpassword  
> **username_0**: don't know why on windows either with bitcoin-core or bitoind I cannot update and set the bitcoin.conf file. It is always ignored  
> **username_1**: Are you putting the bitcoin.conf file in the right place? Are you restarting Bitcoin between attempts to configure it? Can you use -conf at command line to manually set the path to the configuration file?  
> **username_0**: I already did all of that hundreds of times ...  
> **username_2**: @username_0 Do you literally mean you added "-rpcuser" and "-rpcpassword" by themselves as command line options?  Or do you mean that you put the actual username and password?  Or the options followed by the username and password?  I searched and don't see any documentation on those command line options so not sure how I should implement them to fix the same 401 problem.  

---

## 1 answers

### Answer by username_3 on 2017-01-27. Score: 1

`$bitcoin = new Bitcoin("rpcuser","rpcpassword","localhost","80");`

Port 80 is HTTP - it should be 8332 (or 18332 for testnet) unless you have set another RPC port in the configuration file

> **Comments:**
> **username_0**: I tried several ports : 8332 8333 80  

---

