Author

Topic: BlockChain API - PHP HERE (Read 562 times)

newbie
Activity: 24
Merit: 0
March 20, 2014, 04:05:20 PM
#1
Here is something to get you started if you were having issues with sendmany and other API calls at block chain

Accepting Donations: 1Lux3FWAAoNGHA7Hm1JNuBEuKjsTkezBf9

Code:
        define'GUID''USER ID HERE');
        
define'FPASSWORD''FIRSTPASSWORD HERE');
        
define'SPASSWORD''SECOND PASSWORD HERE');
        
        
// Examples
        //Get Balance
        
$BlockChain = new BlockChain;
        
        
//Will return json_encoded array
        
$Balance $BlockChain->BlockChainAPI('balance');
        echo 
"Encoded Balance: " $Balance;
        echo 
"
"
;
        
        
//Decode and get value
        
$Balance json_decode($Balance);
        echo 
"Decoded Balance: " $Balance->balance;
        echo 
"
"
;
        

        
//SendMany
        //Array = 'Address' => 'BTCAmount converted to Satoshi'
        
$Addresses = Array('1Lux3FWAAoNGHA7Hm1JNuBEuKjsTkezBf9' => '10000000''1Lux3FWAAoNGHA7Hm1JNuBEuKjsTkezBf9' => '10000000');
        
$SendMany $BlockChain->BlockChainAPI('sendmany'$Addresses);
        echo 
"Encoded send many return: " $SendMany;
        
// Decode and get value
        
$SendMany json_decode($SendMany);
        echo 
"Message: " $SendMany->message;
        echo 
"Error: " $SendMany->error;
        
        
        
//Generating a new address
        
$RandomLabel "Get btc address";
        
$Address $BlockChain->BlockChainAPI('new_address', array('label' => "$RandomLabel"));
        
//Return json_encoded Array
        
echo "Encoded Address: " $Address;
        
        
// Decode Address
        
$Address json_decode($Address);
        echo 
"New Address: " $Address->address;
        echo 
"Label for Address: " $Address->label;
        
        
        
Class 
BlockChain{
    function 
BlockChainAPI($Param$Param1 null) {
// API settings
static $ch null;
if (is_null($ch)) {
    $ch curl_init();
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_USERAGENT'Mozilla/4.0 (compatible; BlockChain PHP client;)');
}
curl_setopt($chCURLOPT_URL"https://blockchain.info/merchant/" GUID "/$Param?password=" .FPASSWORD "&second_password=" SPASSWORD);
        
        if(
$Param1){
            
$data http_build_query($Param1);
            
curl_setopt($chCURLOPT_POSTFIELDS$data);
        }
        
curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
$Return curl_exec($ch);
return $res;
    }
}
?>

Jump to: