<p>Howdy? </p>

<p>Ok I was searching for tutorials all over internet on "how to integrate bitcoin daemon with php" I came up with many and none of them helped me to get it working flawlessly, some of them threw unexpected and strange errors after 90% completion and some of them good enough to throw error in starting only which saved my time and efforts. Now that I have done what I was looking for why not create a step by step tutorial to help fellow members. So let's get started.</p>

<p>Before we start you should install tmux which helps you save your work even if you are disconnected from the vps. To install tmux:</p>

<pre><code>sudo apt-get install tmux
</code></pre>

<p>to start tmux simply type tmux and to get back on mai screen press ctrl b and then d to restore the session again type tmux attach.</p>

<p>I am expecting you have a Ubuntu 12.x or later because that is what i used for installation. Log in to your VPS or your computer's terminal and install python software properties by typing following command : </p>

<pre><code>sudo apt-get install python-software-properties
</code></pre>

<p>then add repository ppa:bitcoin/bitcoin by typing following command</p>

<pre><code>sudo add-apt-repository ppa:bitcoin/bitcoin
</code></pre>

<p>In my case this didn't work and threw an error stating that add-apt-repository command not found. In ubuntu later version versions this command is not found under python-software-properties package, like in previous versions, but available in software-properties-common. So I had installed it by running these commands :</p>

<pre><code>sudo aptitude install software-properties-common
</code></pre>

<p>To find in which package the command we are looking for exists the following will be of help:</p>

<pre><code>sudo apt-fille update
</code></pre>

<p>Unfortunately in my case this was also not installed so I had installed it by running following command :</p>

<pre><code>apt-get install apt-file
</code></pre>

<p>Now I got the message "The file is up to date"</p>

<p>Now run 
    sudo apt-file search add-apt-repository</p>

<p>You will be prompted to confirm this installation, press Enter key to confirm. </p>

<p>Now run </p>

<pre><code>sudo aptitude update
</code></pre>

<p>and then 
    sudo aptitude install bitcoind</p>

<p>Make a new directory by typing :</p>

<pre><code>mkdir ~/.bitcoin
cd ~/.bitcoin
</code></pre>

<p>Create a new file inside this directory named bitcoin.conf by typing:
    nano bitcoin.conf</p>

<p>insert the following code in the text file:</p>

<pre><code>server=1
daemon=1
rpcuser=any_username
rpcpassword=any_password
</code></pre>

<p>Exit by pressing ctrl+x to confirm the changes press Y and then press return key. Start the bitcoin daemon by typing:</p>

<pre><code>bitcoind
</code></pre>

<p>Bitcoin daemon is now installed and runing you can check it by running the following command </p>

<pre><code>bitcoind getinfo
</code></pre>

<p><strong>Now we will install apache and php on our server</strong></p>

<p>To install apache we will run th following code:</p>

<pre><code>sudo apt-get install apache2
</code></pre>

<p>once apache is installed verify it by typing your ip address in the browser it will say <strong>It works!</strong></p>

<p>now we will install php by following command:</p>

<pre><code>sudo apt-get install php5 libapache2-mod-php5
</code></pre>

<p>now navigate to var/www folder and download jsonRPCClient.php with the following command:</p>

<pre><code>wget http://pmtocoins.com/JSON-RPC_PHP_light.zip
</code></pre>

<p>once downloaded unzip it by typing:
    unzip JSON*.zip</p>

<p>no move to the directory by:
    cd json*/includes</p>

<p>and move jsonRPCClient.php into your www folder</p>

<p>now create a new index.php file in www folder with following code </p>

<pre><code>&lt;?php
include_once('jsonRPCClient.php');
$bitcoin = new jsonRPCClient('http://your_username:your_password@127.0.0.1:8332/');
print_r($bitcoin-&gt;getinfo());
?&gt;
</code></pre>

<p>and you are done navigate to your ip address on the browser e.g 161.232.12.1/index.php</p>

<p>You can have a look on API call list here : <a href="https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list" rel="nofollow">https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list</a></p>

CC BY-SA 3.0
2014-01-12T17:50:37.967Z
2014-01-12T17:50:37.967Z
Khan Shahrukh
<p>.
You can use Open Source Bitcoin-PHP Payment library - </p>

<p><a href="https://github.com/cryptoapi/Payment-Gateway" rel="nofollow">https://github.com/cryptoapi/Payment-Gateway</a></p>

<pre><code>    &lt;?
        require_once( "cryptobox.class.php" );

        $options = array( 
        "private_key" =&gt; "",        // private key from gourl.io
        "orderID"     =&gt; "your_product1_or_signuppage1_etc",
        "amountUSD"   =&gt; 2          // 2 USD
        );  

        // Initialise Payment Class
        $box1 = new Cryptobox ($options);

        // Display Payment Box or successful payment result   
        $paymentbox = $box1-&gt;display_cryptobox();

        // A. Process Received Payment
        if ($box1-&gt;is_paid()) 
        { 
            // Your code here to handle a successful cryptocoin payment
            // ...
        }  
        else $message .= "The payment has not been made yet";
    ?&gt;
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;&lt;head&gt;&lt;/head&gt;
    &lt;body&gt;
    &lt;?= $paymentbox ?&gt;
    &lt;?= $message ?&gt;
    &lt;/body&gt;
    &lt;/html&gt;
</code></pre>

CC BY-SA 3.0
2014-10-30T15:32:31.963Z
This doesn't solve the asker's problem. He wants to communicate with his own Bitcoin daemon
Nick ODell
CC BY-SA 3.0
2014-10-30T15:10:27.940Z
2014-10-30T15:10:27.940Z
webdev1
<p>I have installed the daemons of Bitcoin, <a href="https://en.wikipedia.org/wiki/Litecoin" rel="nofollow">Litecoin</a> and <a href="https://en.wikipedia.org/wiki/Dogecoin" rel="nofollow">Dogecoin</a> on the same server. All three of them are up and running, but the Bitcoin daemon does not reply with anything when I run <code>bitcoind getinfo</code>. It may be network issues or something like that. I have installed PHP on the same server and created an <code>index.php</code> file in the <code>/var/www/</code> folder.</p>

<p>Content of <code>index.php</code> file:</p>

<pre><code>&lt;?php
    include_once('Bitcoin.php');
    include_once('jsonRPCClient.php');
    $dogecoin = new jsonRPCClient('http://user:pass@localhost:8332/');
    print_r($dogecoin-&gt;getinfo());
?&gt;
</code></pre>

<p>Content of Bitcoin.php:</p>

<pre><code>&lt;?php
    class Bitcoin (
        // @var string
        private $username;
        // @var string
        private @password;
        // @var string
        private $url;
        // @var string
        private $id;

        public function __construct($url,$username,$password)
        {
            $this-&gt;url=$url;
            $this-&gt;username = $username;
            $this-&gt;password = $password;
            $this-&gt;id = 1;
        }

        public function __call($method,$params)
        {
            $params = array_values($params);

            $request = json_encode(array(
                                       'method' =&gt;strtolower($method),
                                       'params' =&gt; $params,
                                       'id' =&gt; $this-&gt; id));
            $curl = curl_init();
            curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content type:application/json"));
            curl_setopt($curl, CURLOPT_URL, $this-&gt;url);
            curl_setopt($curl, CURLOPT_USERPWD, $this-&gt;username.":".$this-&gt;password);
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
            $response = curl_exec($curl);
            curl_close($curl);

            if(!$resposne)
            {
                throw new Exception('Unable to connect to '.$this-&gt;url, 0);
            }
            $response = json_decode($response,true);
        }

        Then there are a condition to throw a different error upon a different situation.

    }
</code></pre>

CC BY-SA 3.0
2014-01-12T17:55:59.580Z
@peter-mortensen that extra lines are just to avoid typing all the exceptions it was not like this in the actual file
Khan Shahrukh
<bitcoind><php>
Problem integrating Bitcoin daemon with PHP
Khan Shahrukh
# Problem integrating Bitcoin daemon with PHP

(Asked by: username_0 on 2014-01-11)

I have installed the daemons of Bitcoin, [Litecoin](https://en.wikipedia.org/wiki/Litecoin) and [Dogecoin](https://en.wikipedia.org/wiki/Dogecoin) on the same server. All three of them are up and running, but the Bitcoin daemon does not reply with anything when I run `bitcoind getinfo`. It may be network issues or something like that. I have installed PHP on the same server and created an `index.php` file in the `/var/www/` folder.

Content of `index.php` file:

```
<?php
    include_once('Bitcoin.php');
    include_once('jsonRPCClient.php');
    $dogecoin = new jsonRPCClient('http://user:pass@localhost:8332/');
    print_r($dogecoin->getinfo());
?>

```

Content of Bitcoin.php:

```
<?php
    class Bitcoin (
        // @var string
        private $username;
        // @var string
        private @password;
        // @var string
        private $url;
        // @var string
        private $id;

        public function __construct($url,$username,$password)
        {
            $this->url=$url;
            $this->username = $username;
            $this->password = $password;
            $this->id = 1;
        }

        public function __call($method,$params)
        {
            $params = array_values($params);

            $request = json_encode(array(
                                       'method' =>strtolower($method),
                                       'params' => $params,
                                       'id' => $this-> id));
            $curl = curl_init();
            curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content type:application/json"));
            curl_setopt($curl, CURLOPT_URL, $this->url);
            curl_setopt($curl, CURLOPT_USERPWD, $this->username.":".$this->password);
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
            $response = curl_exec($curl);
            curl_close($curl);

            if(!$resposne)
            {
                throw new Exception('Unable to connect to '.$this->url, 0);
            }
            $response = json_decode($response,true);
        }

        Then there are a condition to throw a different error upon a different situation.

    }

```

**Tags:** bitcoind, php

> **Comments:**
> **username_0**: @peter-mortensen that extra lines are just to avoid typing all the exceptions it was not like this in the actual file  

---

## 2 answers

### Answer by username_0 on 2014-01-12. Score: 0 | Accepted answer

Howdy?

Ok I was searching for tutorials all over internet on "how to integrate bitcoin daemon with php" I came up with many and none of them helped me to get it working flawlessly, some of them threw unexpected and strange errors after 90% completion and some of them good enough to throw error in starting only which saved my time and efforts. Now that I have done what I was looking for why not create a step by step tutorial to help fellow members. So let's get started.

Before we start you should install tmux which helps you save your work even if you are disconnected from the vps. To install tmux:

```
sudo apt-get install tmux

```

to start tmux simply type tmux and to get back on mai screen press ctrl b and then d to restore the session again type tmux attach.

I am expecting you have a Ubuntu 12.x or later because that is what i used for installation. Log in to your VPS or your computer's terminal and install python software properties by typing following command :

```
sudo apt-get install python-software-properties

```

then add repository ppa:bitcoin/bitcoin by typing following command

```
sudo add-apt-repository ppa:bitcoin/bitcoin

```

In my case this didn't work and threw an error stating that add-apt-repository command not found. In ubuntu later version versions this command is not found under python-software-properties package, like in previous versions, but available in software-properties-common. So I had installed it by running these commands :

```
sudo aptitude install software-properties-common

```

To find in which package the command we are looking for exists the following will be of help:

```
sudo apt-fille update

```

Unfortunately in my case this was also not installed so I had installed it by running following command :

```
apt-get install apt-file

```

Now I got the message "The file is up to date"

Now run
sudo apt-file search add-apt-repository

You will be prompted to confirm this installation, press Enter key to confirm.

Now run

```
sudo aptitude update

```

and then
sudo aptitude install bitcoind

Make a new directory by typing :

```
mkdir ~/.bitcoin
cd ~/.bitcoin

```

Create a new file inside this directory named bitcoin.conf by typing:
nano bitcoin.conf

insert the following code in the text file:

```
server=1
daemon=1
rpcuser=any_username
rpcpassword=any_password

```

Exit by pressing ctrl+x to confirm the changes press Y and then press return key. Start the bitcoin daemon by typing:

```
bitcoind

```

Bitcoin daemon is now installed and runing you can check it by running the following command

```
bitcoind getinfo

```

**Now we will install apache and php on our server**

To install apache we will run th following code:

```
sudo apt-get install apache2

```

once apache is installed verify it by typing your ip address in the browser it will say **It works!**

now we will install php by following command:

```
sudo apt-get install php5 libapache2-mod-php5

```

now navigate to var/www folder and download jsonRPCClient.php with the following command:

```
wget http://pmtocoins.com/JSON-RPC_PHP_light.zip

```

once downloaded unzip it by typing:
unzip JSON\*.zip

no move to the directory by:
cd json\*/includes

and move jsonRPCClient.php into your www folder

now create a new index.php file in www folder with following code

```
<?php
include_once('jsonRPCClient.php');
$bitcoin = new jsonRPCClient('http://your_username:your_password@127.0.0.1:8332/');
print_r($bitcoin->getinfo());
?>

```

and you are done navigate to your ip address on the browser e.g 161.232.12.1/index.php

You can have a look on API call list here : <https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list>


---

### Answer by username_1 on 2014-10-30. Score: -1

.
You can use Open Source Bitcoin-PHP Payment library -

<https://github.com/cryptoapi/Payment-Gateway>

```
    <?
        require_once( "cryptobox.class.php" );

        $options = array( 
        "private_key" => "",        // private key from gourl.io
        "orderID"     => "your_product1_or_signuppage1_etc",
        "amountUSD"   => 2          // 2 USD
        );  

        // Initialise Payment Class
        $box1 = new Cryptobox ($options);

        // Display Payment Box or successful payment result   
        $paymentbox = $box1->display_cryptobox();

        // A. Process Received Payment
        if ($box1->is_paid()) 
        { 
            // Your code here to handle a successful cryptocoin payment
            // ...
        }  
        else $message .= "The payment has not been made yet";
    ?>
    <!DOCTYPE html>
    <html><head></head>
    <body>
    <?= $paymentbox ?>
    <?= $message ?>
    </body>
    </html>

```

> **Comments:**
> **username_2**: This doesn't solve the asker's problem. He wants to communicate with his own Bitcoin daemon  

---

