Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: MangoJ on July 04, 2013, 10:05:16 PM



Title: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 04, 2013, 10:05:16 PM
Does anyone have an example PHP script for reading my wallet transactions?
Or for creating new BTC addresses, sending BTC?

I can code in SQL and PHP, HTML, XML.. but I dont get how to use JSON, or anything with BTC.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: elwan on July 04, 2013, 10:18:47 PM
what kind of wallet you use ?? maybe i can help you  ::)


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 04, 2013, 11:17:56 PM
I use Bitcoin-Qt on a Mac.

Help would be very appreciated.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: Brunic on July 04, 2013, 11:26:15 PM
json_decode($yourJSONstring, true)

http://php.net/manual/fr/function.json-decode.php


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 04, 2013, 11:43:28 PM
json_decode($yourJSONstring, true)

http://php.net/manual/fr/function.json-decode.php

What does that mean?
I do not know how to use JSON.  I don't know what it is.
Your link is in french, I changed it to English, but that is just another bit of helpfulness.
In English, it still does not tell me anything about how to use JSON for a webserver of Bitcoin transactions.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: Brunic on July 05, 2013, 01:58:11 AM
json_decode($yourJSONstring, true)

http://php.net/manual/fr/function.json-decode.php

What does that mean?
I do not know how to use JSON.  I don't know what it is.
Your link is in french, I changed it to English, but that is just another bit of helpfulness.
In English, it still does not tell me anything about how to use JSON for a webserver of Bitcoin transactions.

JSON is simply a string of data. The JSON encoding is just a way to share this data easily.

You can make the test yourself. Create a PHP array, put some data in it. Use something like:
$string = json_encode($array);
var_dump($string);

and you'll see your $array encoded in JSON. To get it back in an array format, you do:
$array = json_decode($string, true);

and you will have your array back.

With JSON, servers can simply broadcast data in a format that any language can read and decode easily.

Sorry for the french link BTW, I sometimes mix up languages.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 05, 2013, 04:10:17 AM
json_decode($yourJSONstring, true)

http://php.net/manual/fr/function.json-decode.php

What does that mean?
I do not know how to use JSON.  I don't know what it is.
Your link is in french, I changed it to English, but that is just another bit of helpfulness.
In English, it still does not tell me anything about how to use JSON for a webserver of Bitcoin transactions.

JSON is simply a string of data. The JSON encoding is just a way to share this data easily.

You can make the test yourself. Create a PHP array, put some data in it. Use something like:
$string = json_encode($array);
var_dump($string);

and you'll see your $array encoded in JSON. To get it back in an array format, you do:
$array = json_decode($string, true);

and you will have your array back.

With JSON, servers can simply broadcast data in a format that any language can read and decode easily.

Sorry for the french link BTW, I sometimes mix up languages.


Thanks.
I figured that out reading into it.
Now my problem is, how do I send any data requests to my wallet to get real time data, and make transactions?
curl in php?


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: twobits on July 05, 2013, 05:10:09 AM
json_decode($yourJSONstring, true)

http://php.net/manual/fr/function.json-decode.php

What does that mean?
I do not know how to use JSON.  I don't know what it is.
Your link is in french, I changed it to English, but that is just another bit of helpfulness.
In English, it still does not tell me anything about how to use JSON for a webserver of Bitcoin transactions.

JSON is simply a string of data. The JSON encoding is just a way to share this data easily.

You can make the test yourself. Create a PHP array, put some data in it. Use something like:
$string = json_encode($array);
var_dump($string);

and you'll see your $array encoded in JSON. To get it back in an array format, you do:
$array = json_decode($string, true);

and you will have your array back.

With JSON, servers can simply broadcast data in a format that any language can read and decode easily.

Sorry for the french link BTW, I sometimes mix up languages.


Thanks.
I figured that out reading into it.
Now my problem is, how do I send any data requests to my wallet to get real time data, and make transactions?
curl in php?


Yeah, it is just using http or https as the transport, so curl works just fine for it.

Alternatively, I have https://github.com/mikegogulski/bitcoin-php bookmarked to try if I ever wind up needing to do what you want to do, to save me some time rolling my own.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: domob on July 05, 2013, 06:00:30 AM
Now my problem is, how do I send any data requests to my wallet to get real time data, and make transactions?
curl in php?

An alternative if you want to communicate to the local bitcoind on your server is to use the bitcoind executable client via system() or popen().  If you are interested in this solution, I can send you my PHP code I use for that.  Honestly though, I believe HTTP is probably an easier way to communicate with it.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 05, 2013, 06:48:39 AM

Yeah, it is just using http or https as the transport, so curl works just fine for it.

Alternatively, I have https://github.com/mikegogulski/bitcoin-php bookmarked to try if I ever wind up needing to do what you want to do, to save me some time rolling my own.


Do you know where I could find an example of an http transport?  or a curl example?
I don't get how I can transport anything over curl or http when it is on the same server...
that would be talking to itself.
What address? 127.1.1.1? What port? How is Bitcoin even already active listening on that port?

I believe I am lost.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 05, 2013, 06:49:35 AM
Now my problem is, how do I send any data requests to my wallet to get real time data, and make transactions?
curl in php?

An alternative if you want to communicate to the local bitcoind on your server is to use the bitcoind executable client via system() or popen().  If you are interested in this solution, I can send you my PHP code I use for that.  Honestly though, I believe HTTP is probably an easier way to communicate with it.

Yes I would love to see your PHP code as an example.
I have no idea how bitcoin is suppost to be turned on to listen to incoming requests.  Or where to request them.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: 🏰 TradeFortress 🏰 on July 05, 2013, 09:31:51 AM
Get jsonrpcclient. Connect to bitcoind RPC. The data is automatically decoded.

Code:
$transactions = $rpc->listtransactions();
var_dump($transactions);

foreach($transactions as $tx){
    echo "TXID: $tx <br />";
}

Alternatively, try Inputs.io's API (https://inputs.io/api) which is incredibly easy to use. Example callback PHP code is already given to you, you don't need to deal with running your own bitcoind. You can just set a callback URL and get the new transactions pushed to you.

(Btw, WTF is in your linked youtube video? (http://i.ytimg.com/vi/wrJVzvJO7EE/0.jpg) ???)


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: twobits on July 05, 2013, 11:20:08 AM
Get jsonrpcclient. Connect to bitcoind RPC. The data is automatically decoded.


I recommend against this for three reasons.

Firstly, unpatched, it can display your login information on errors.
Secondly, it does not work with php 5.4
Thirdly, it is GPLed so anything you make using it has to be GPLed as well.




Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: twobits on July 05, 2013, 11:22:58 AM

Yeah, it is just using http or https as the transport, so curl works just fine for it.

Alternatively, I have https://github.com/mikegogulski/bitcoin-php bookmarked to try if I ever wind up needing to do what you want to do, to save me some time rolling my own.


Do you know where I could find an example of an http transport?  or a curl example?
I don't get how I can transport anything over curl or http when it is on the same server...
that would be talking to itself.
What address? 127.1.1.1? What port? How is Bitcoin even already active listening on that port?

I believe I am lost.

yes,  http://localhost:xxxx or http://127.0.0.1:xxxx,  bitcoin is listening if you run it with -server and/or -daemon.  I dont recall the default port number of hand, but it is in the wiki or source code.   You can also set it in bitcoin.conf with rpcport=1234


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 06, 2013, 01:57:00 AM
Get jsonrpcclient. Connect to bitcoind RPC. The data is automatically decoded.

Code:
$transactions = $rpc->listtransactions();
var_dump($transactions);

foreach($transactions as $tx){
    echo "TXID: $tx <br />";
}

Alternatively, try Inputs.io's API (https://inputs.io/api) which is incredibly easy to use. Example callback PHP code is already given to you, you don't need to deal with running your own bitcoind. You can just set a callback URL and get the new transactions pushed to you.

(Btw, WTF is in your linked youtube video? (http://i.ytimg.com/vi/wrJVzvJO7EE/0.jpg) ???)

Thank you but I wish to have everything on my own server.
Otherwise I need to spend a lot of time and effort to decide if I can trust an outside server under the control of god-knows, with the control of my bitcoins. 

My youtube video is just a video of onw of my GPU miners ;)  ha ha


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 06, 2013, 01:58:39 AM
Get jsonrpcclient. Connect to bitcoind RPC. The data is automatically decoded.


I recommend against this for three reasons.

Firstly, unpatched, it can display your login information on errors.
Secondly, it does not work with php 5.4
Thirdly, it is GPLed so anything you make using it has to be GPLed as well.




Agreed.



Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: MangoJ on July 06, 2013, 02:20:15 AM

yes,  http://localhost:xxxx or http://127.0.0.1:xxxx,  bitcoin is listening if you run it with -server and/or -daemon.  I dont recall the default port number of hand, but it is in the wiki or source code.   You can also set it in bitcoin.conf with rpcport=1234


Ok, I can try that.  I just need to find out how to open bitcoin with -server or -daemon flags using the Terminal in Mac.


Title: Re: I can code PHP, but how do I use JSON for bitcoin transactions?
Post by: 🏰 TradeFortress 🏰 on July 06, 2013, 02:40:48 AM
Get jsonrpcclient. Connect to bitcoind RPC. The data is automatically decoded.


I recommend against this for three reasons.

Firstly, unpatched, it can display your login information on errors.
Secondly, it does not work with php 5.4
Thirdly, it is GPLed so anything you make using it has to be GPLed as well.
GPL licenses does not work like that.

If you don't have error reporting set to none on your production server, well....