Bitcoin Forum
December 15, 2024, 09:06:07 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Blockchain API sendmany PLS HELP!  (Read 2751 times)
dhavin (OP)
Hero Member
*****
Offline Offline

Activity: 683
Merit: 500


View Profile
November 07, 2014, 04:12:52 AM
 #1

Here's a practical example with php/MySQL.

Lets say for the sake of argument you have a MySQL database table like this:
Code:
CREATE TABLE `bitcoin_payments` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `bitcoin_address` varchar(34) NOT NULL,
  `payment_in_satoshi` bigint(20) unsigned NOT NULL DEFAULT '0',
  `transaction_hash` varchar(64) NOT NULL,
  PRIMARY KEY (`id`)
);

And in this table you have a list of payments to be made. If the payment has already been made then the transaction_hash field contains the transaction hash the payment was made with, otherwise it's empty.

Code:
<?php

$guid
="GUID_HERE";
$firstpassword="PASSWORD_HERE";
$secondpassword="PASSWORD_HERE";

// Your MySQL database login credentials here
$host="localhost"// MySQL host
$user=""// MySQL user
$pass=""// MySQL password for user above
$db=""// MySQL database to use

mysql_select_db($dbmysql_connect($host$user$pass));

$result=mysql_query("SELECT `id`, `bitcoin_address`, `payment_in_satoshi` FROM `bitcoin_payments` WHERE `transaction_hash`='' ORDER BY `id` ASC");
$recipient_list=array();
$new_paid_ids="";
if(
mysql_num_rows($result)>0)
{
    while(
$row=mysql_fetch_assoc($result))
    {
        
$new_paid_ids.=$row["id"].",";
        
$recipient_list[$row["bitcoin_address"]]=$row["payment_in_satoshi"];
    }
    
$new_paid_ids=trim($new_paid_ids",");
}
if(
count($recipient_list)>0)
{
    
$recipients=urlencode(json_encode($recipient_list));
    
$json_url "http://blockchain.info/merchant/$guid/sendmany?password=$firstpassword&second_password=$secondpassword&recipients=$recipients";
    
$json_data file_get_contents($json_url);
    if(
$http_response_header[0]=="HTTP/1.1 200 OK")
    {
        
$json_feed json_decode($json_data);
        
$error $json_feed->error;
        if(
is_null($error))
        {
            
$message $json_feed->message;
            
$txid $json_feed->tx_hash;
            
mysql_query("UPDATE `bitcoin_address` SET `transaction_hash`='".mysql_real_escape_string($txid)."' WHERE `id` IN(".$new_paid_ids.")");
            echo 
$message." - <a href='https://blockchain.info/tx/".$txid."'>".$txid."</a>";
        }
        else
            echo 
$error;
    }
    else
        echo 
"bitchain.info returned the following http error:<br /><br />".$http_response_header[0];
}

?>

Please note the above is not tested but it should work in theory. Wink

So this old thread is the closest I can find to a working PHP example of sendmany using Blockchain's API. I cannot get it to work, it's driving me completely batty. I've got the right passwords and guid and I'm using the code posted above and I get back "Unknown Error" from the API. Can anyone take a moment to look at the code, maybe you can see something I missed?

Very much appreciated!

http://www.scratch4satoshis.com/ - 1600 satoshi/hour 2 MILLION SATOSHI minimum weekly lottery draw! Get your tickets submitted!
TheBeardedOne
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile
November 07, 2014, 11:46:03 AM
 #2

First I believe that the mysql statements that you are using are deprecated.

http://php.net/manual/en/function.mysql-select-db.php

You should look into PDO's. As for what you are doing with the array, if you are already looping through it you may as well build a JSON string right off the bat - i mean I wouldn't do it personally but it gets the job done and there is really nothing wrong with it.

Using the sendmany example here:

https://blockchain.info/api/blockchain_wallet_api

Then just call URL encode.

One thing that comes to the top of my head, this being from experience using qt, do you have an address listed twice in the send many data? I know that qt does not allow this when using rpc.
dhavin (OP)
Hero Member
*****
Offline Offline

Activity: 683
Merit: 500


View Profile
November 07, 2014, 01:10:38 PM
 #3

The urlencoded output of the array is: (i just manually threw some spaces in there to improve readability)

%7B%22 1CagL2pgL73cs4LBCq82RYUunXJUw4yXBz %22%3A%22 5500 %22%2C%22 19rbySNBMmYvhmUgpBLGbqHXzJDBotchSy %22%3A%22 5500 %22%7D

So it's two different addresses. Encoding looks correct..
7B = {
3A = :
2C = , and
7D = }

Any other thoughts?

http://www.scratch4satoshis.com/ - 1600 satoshi/hour 2 MILLION SATOSHI minimum weekly lottery draw! Get your tickets submitted!
TheBeardedOne
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile
November 07, 2014, 03:19:53 PM
 #4

Remove the quotation marks " " around the amount sent. Only the address should be within their own "".

EDIT: Think you need to cast the $row["payment_in_satoshi"] to int, intval() i believe is the function.
dhavin (OP)
Hero Member
*****
Offline Offline

Activity: 683
Merit: 500


View Profile
November 07, 2014, 04:27:42 PM
 #5

Remove the quotation marks " " around the amount sent. Only the address should be within their own "".

EDIT: Think you need to cast the $row["payment_in_satoshi"] to int, intval() i believe is the function.

The quotations didn't matter, intval() nailed it. Thank you!

http://www.scratch4satoshis.com/ - 1600 satoshi/hour 2 MILLION SATOSHI minimum weekly lottery draw! Get your tickets submitted!
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!