Bitcoin Forum

Economy => Service Discussion => Topic started by: likl3sfuhjh on April 25, 2013, 07:27:07 PM



Title: blockchain.info api multi send HELP
Post by: likl3sfuhjh on April 25, 2013, 07:27:07 PM

This code segment is from http://blockchain.info/api/blockchain_wallet_api
How do i execute this? what do i need to install, do i need to link to a json library or something. I need a step thru process here.  Thanks

Code:
<?

$guid="GUID_HERE";
$firstpassword="PASSWORD_HERE";
$secondpassword="PASSWORD_HERE";
$amounta = "10000000";
$amountb = "400000";
$addressa = "1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq";
$addressb = "1ExD2je6UNxL5oSu6iPUhn9Ta7UrN8bjBy";
$recipients = urlencode('{
                  "'.$addressa.'": '.$amounta.',
                  "'.$addressb.'": '.$amountb.'
               }');

$json_url = "http://blockchain.info/merchant/$guid/sendmany?password=$firstpassword&second_password=$secondpassword&recipients=$recipients";

$json_data = file_get_contents($json_url);

$json_feed = json_decode($json_data);

$message = $json_feed->message;
$txid = $json_feed->tx_hash;

?>


Title: Re: blockchain.info api multi send HELP
Post by: scintill on April 26, 2013, 10:38:51 PM
I think it would be more efficient for you to answer, what errors are you experiencing?

The json module seems to be enabled by default in recent PHP versions.  The only other dependency is on the http file wrapper being enabled, in the file_get_contents call.  What is the output of var_dump(stream_get_wrappers()) ?


Title: Re: blockchain.info api multi send HELP
Post by: Petr1fied on April 27, 2013, 05:36:42 PM
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. ;)


Title: Re: blockchain.info api multi send HELP
Post by: likl3sfuhjh on April 28, 2013, 02:15:53 AM
how do you send the data


Title: Re: blockchain.info api multi send HELP
Post by: Petr1fied on April 28, 2013, 03:10:35 AM
The data is sent in the file_get_contents() line. (Line 31 in the example above). It's the equivalent of you opening the url stored in the variable $json_url in your Web browser. Assuming you get a 200 response and no error is returned, the funds will have been sent at this point.


Title: Re: blockchain.info api multi send HELP
Post by: likl3sfuhjh on April 28, 2013, 03:14:48 AM
ok thought so thanks


Title: Re: blockchain.info api multi send HELP
Post by: Petr1fied on April 28, 2013, 03:18:23 AM
No problem.  ;)


Title: Re: blockchain.info api multi send HELP
Post by: likl3sfuhjh on April 28, 2013, 03:45:43 AM
removed