Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: cosmicboy on March 27, 2014, 04:00:23 PM



Title: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: cosmicboy on March 27, 2014, 04:00:23 PM
edit: thanks for the advice all but I was looking for the actual code. Just hired someone on fiverr to write it. the curl and other extraneous code was from previous attempts.


Hey all. I don't know much about php but have been trying to get this api script working. This is the callback script which is supposed to send two letters after bitcoin has been sent with one confirmation: one to me and one to the customer. I am having a hard time getting this to send an email after just one confirmation. If I take this part out: "if ($_GET['confirmations'] == 1)" then it will send me emails every hour with different confirmation amounts. But if I put this qualifier in it sends me nothing. I am guessing that maybe when the script is kicks in the number of confirmations might be over 1 already? How do I solve this to get the script to send just one email after one confirmation? First person to reply with fix and their wallet address gets reward.


<?php

/**
 * Callback endpoint for the Blockchain Receive API.
 *
 * Notifies the administrator when a watched address has a new transaction.
 */

if ($_GET['confirmations'] < 1) {
  die('Waiting for 1 notification.');
}



$to = 'marley4567@gmail.com';
$subject = 'New transaction through Blockchain API';

$message = 'There is a new transaction detected by the Blockchain API.' . "\r\n\r\n";
$message .= 'Amount:           ' . sprintf('%02.8f', ($_GET['value'] / 100000000)) . ' BTC'. "\r\n";
$message .= 'Address:          ' . $_GET['input_address'] . "\r\n";
$message .= 'Confirmations:    ' . $_GET['confirmations'] . "\r\n";
$message .= 'Track:            http://blockchain.info/tx/' . $_GET['transaction_hash'] . "\r\n";
$message .= 'Customer\'s email: ' . $_GET['email'] . "\r\n";
$headers = 'From: "Blockchain API endpoint" <noreply@noreply.com>';

$fields = array(
  'to' => $to,
  'subject' => $subject,
  'message' => $message,
  'headers' => $headers,
);

$postvars = http_build_query($fields);

if ($_GET['confirmations'] == 1) {
   mail($to, $subject, $message, $headers);
}


curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$res = curl_exec($ch);

if ($res == 'ok') {
  echo '*ok*';
}
else {
  echo 'Mail not sent!';
  echo $res;
}

if (isset($_GET['email'])) {
  $to = $_GET['email'];
  $subject = 'Payment Verification';
  $message = 'This is an automatic notification that I have received a payment for your order.' . "\r\n\r\n";
  $message .= 'Your item should arrive in the mail within a week.';
  $headers = 'From: "Marley" <marley4567@gmail.com>';
 
  $fields = array(
    'to' => $to,
    'subject' => $subject,
    'message' => $message,
    'headers' => $headers,
  );

  $postvars = http_build_query($fields);

if ($_GET['confirmations'] == 1) {
   mail($to, $subject, $message, $headers);
}


  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

  $res = curl_exec($ch);
}


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: elbandi on March 27, 2014, 05:22:11 PM
store somewhere (session or database) if you already sent the mail.

Elbandi


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: cosmicboy on March 27, 2014, 07:17:35 PM
is there simple code for that? I have no idea how to do it. willing to pay more if its more work..I thought this would be a simple fix


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: leckey on March 27, 2014, 08:47:57 PM
What I'd do, is store transactions in a database table. Check the tx hash of the transaction hitting the callback, if it doesn't exist in the database insert it and send the email. If it DOES exist in the database, just either ignore it, or update it with a different confirmation count.

Edit: Also, why are you using cURL here? Seems needless?


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: gwlloyd on March 28, 2014, 03:33:17 AM
Edit: Also, why are you using cURL here? Seems needless?

I'm confused at the cURL too, perhaps it's supposed to be sending a second notification via a web URL too but that line or two to setup the cURL correctly is missing?.. The code sends the mail() and then passes the same data to cURL straight after.


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: solomon on March 28, 2014, 04:21:10 AM
If its sending once every hour without confirmations=1 then i'm guessing the script is being automated to run once an hour and the email will only be sent when it is told to run AND 'confirmations' is exactly 1, so if its 0 or 2 or more, nothing is be sent. A shitty fix would be to set if confirmations > 0 && < 7. It would work most of the time, except occasionally people will get 2 emails or none if a load of blocks are solved faster or slower than usual. The only proper way to do it is use a database and record when you send the email.


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: leckey on March 28, 2014, 11:24:57 AM
Edit: Also, why are you using cURL here? Seems needless?

I'm confused at the cURL too, perhaps it's supposed to be sending a second notification via a web URL too but that line or two to setup the cURL correctly is missing?.. The code sends the mail() and then passes the same data to cURL straight after.

and $ch isn't even set anywhere....


Title: This message was too old and has been purged
Post by: Evil-Knievel on March 28, 2014, 11:55:44 AM
This message was too old and has been purged


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: elbandi on March 28, 2014, 01:01:49 PM
Use this small adjustment :-)

http://cdn.memegenerator.net/instances/500x/44916487.jpg

 ;D


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: jimbursch on March 31, 2014, 04:42:17 PM
I'm just curious, did this problem get solved and was the bounty paid?


Title: This message was too old and has been purged
Post by: Evil-Knievel on March 31, 2014, 10:30:56 PM
This message was too old and has been purged


Title: Re: Blockchain API Simple PHP Problem - .03 bitcoin reward
Post by: ExchangeCurrency on October 24, 2014, 04:35:16 AM
Hi,
i had alot of problems with this Bitcoin API too, specialy the callback URL
after 2 nights of Headaches,i finaly found the problem :p
this is a DEMO, who is interesting to integrate this LOVELY API on his website he can "Check before Buy" here is the link : http://ex-currency.com/btc/index.php
Payment Sent/Recieved/Confirmed IMMEDIATLY
the price of curiosity is 1 USD