Author

Topic: Wallet notify using a PHP routing system? (Read 262 times)

newbie
Activity: 5
Merit: 0
Thanks for the help guys. However i found the following solution:

walletnotify=curl 'http://localhost:8000/wallet/notify/'%s

which is working perfectly, without the need of a script.
full member
Activity: 198
Merit: 130
Some random software engineer
Can you provide an PHP example? it seems like you want me to link to a file.php (which then takes args as a input), but i'm using controllers without extensions and would like to simply pass it to that :-)

i would really like if i could just do it in one line...

If you want to use Symfony, and your Symfony application is on the same host, you can do it simpler that using the whole curl/http/controller stuff, why don't you try to create and use a custom console Command ?

Don't copy/paste the following example as I did not test it (just wrote like this), but this should work:

Code:

namespace App\Command;

use 
Symfony\Component\Console\Command\Command;
use 
Symfony\Component\Console\Input\InputInterface;
use 
Symfony\Component\Console\Output\OutputInterface;
use 
Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class 
NotifyTransactionCommand extends ContainerAwareCommand
{
    protected function 
configure()
    {
        
$this
            
->setName('my-app:notify-tx')
            ->
setDescription('Call to notify a transaction')
            ->
addArgument('txid'InputArgument::REQUIRED'The Transaction ID.')
        ;
    }

    protected function 
execute(InputInterface $inputOutputInterface $output)
    {
        echo 
"Hey, I got a new transaction id: " $input->get('txid') . "\n";
        
// do other stuff.

    
}
}

Your commande will be usable doing the following:

Code:
php /path/to/your/symfony-app/bin/console my-app:notify-tx THE_TX_ID

Then you'll be able to put in the Bitcoin's bitcoin.conf configuration file:

Code:
walletnotify = php /path/to/your/symfony-app/bin/console my-app:notify-tx %s


If your symfony app is not on the same host than bitcoin's wallet, you have to use curl to reach the external web server. You can do a php script like this (again, wrote it quickly, didn't test it):

Code:

if(count($argv) != 2) {
  echo 
"Usage: " $argv[0] . \n";
  exit(
1);
}

$txid $argv[1];

$ch curl_init();
curl_setopt($chCURLOPT_URL"http://your-symfony-app-url.com/wallet/notify/" $txid);
curl_setopt($chCURLOPT_HEADER0);

curl_exec($ch);

curl_close($ch);

And put in bitcoin.conf:

Code:
walletnotify=php /path/to/your/script.php %s
legendary
Activity: 1624
Merit: 2481
Can you provide an PHP example? it seems like you want me to link to a file.php (which then takes args as a input), but i'm using controllers without extensions and would like to simply pass it to that :-)

Your config or startup command:
Code:
walletnotify = /home/path/to/your/script.php %s

walletnotify does pass the transaction hash to your PHP script.


In your php script:
Code:
$TransactionHash = $argv[1]

Afterwards you can do whatever you want with the TX ID.
Curl example:

Code:
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "https://www.YourURL.com");
curl_exec ($curl);
curl_close ($curl);
More examples using curl: http://www.hackingwithphp.com/15/10/2/your-first-curl-scripts


i would really like if i could just do it in one line...

What exactly are you trying to do? Not everything can be implemented as a one-liner  Tongue
Curl itself (executed inside a php script) does need 4 lines.
newbie
Activity: 5
Merit: 0
but it seems like i have to use a bash script.


You should have read my post more carefully  Wink

You don't have to use a bash script. Python/PHP would work too, or any other scripting language.

Can you provide an PHP example? it seems like you want me to link to a file.php (which then takes args as a input), but i'm using controllers without extensions and would like to simply pass it to that :-)

i would really like if i could just do it in one line...
legendary
Activity: 1624
Merit: 2481
but it seems like i have to use a bash script.


You should have read my post more carefully  Wink

You don't have to use a bash script. Python/PHP would work too, or any other scripting language.
newbie
Activity: 5
Merit: 0
Hi.

I'm running a Symfony4 (PHP) project which makes use of a routing system.

I want to use walletnotify in the following way, but i want to make sure that it's right before testing:

bitcoin.conf:

walletnotify=curl http://127.0.0.1:8000/wallet/notify/%s


Route:

https://i.stack.imgur.com/nBDNl.png

how would i use the argv function in this case and would it work without specifying a .php extension?

argv is for CLI - command line - usage. If you are going through an web server (and you will, if you're using Symfony controllers), $argv will not be defined.

If your Symfony route is /wallet/notify/{txid}, then your function will be most likely be prototyped like the following, and you'll be able to retrieve your transaction id using $txid (given as a parameter in your function):

Code:
class WalletNotifyController Extends Controller
{
  /**
   * @Route("/wallet/notify/{txid}, name="user_notify)
   */
  public function notify($txid)
  {
    // Your transaction id will be in $txid;
    echo $txid;
  }
}

Symfony documentation is pretty much complete, with a lot of samples. You should give it a try.

Yes i realise how Symfony works, the primary concern i had was how i should formulate the walletnotify script to be able to pass the TXID parameter into the controller and if the following would work :-) Thanks!

walletnotify=curl http://127.0.0.1:8000/wallet/notify/%s

but it seems like i have to use a bash script.
full member
Activity: 198
Merit: 130
Some random software engineer
Hi.

I'm running a Symfony4 (PHP) project which makes use of a routing system.

I want to use walletnotify in the following way, but i want to make sure that it's right before testing:

bitcoin.conf:

walletnotify=curl http://127.0.0.1:8000/wallet/notify/%s


Route:



how would i use the argv function in this case and would it work without specifying a .php extension?

argv is for CLI - command line - usage. If you are going through an web server (and you will, if you're using Symfony controllers), $argv will not be defined.

If your Symfony route is /wallet/notify/{txid}, then your function will be most likely be prototyped like the following, and you'll be able to retrieve your transaction id using $txid (given as a parameter in your function):

Code:
class WalletNotifyController Extends Controller
{
  /**
   * @Route("/wallet/notify/{txid}, name="user_notify)
   */
  public function notify($txid)
  {
    // Your transaction id will be in $txid;
    echo $txid;
  }
}

Symfony documentation is pretty much complete, with a lot of samples. You should give it a try.
legendary
Activity: 1624
Merit: 2481
Is there no way to do it directly without the need for a bash script?

walletnotify and blocknotify do run a script if a specific event happens.
You don't have to use a bash script. Python/PHP would work too, or any other scripting language.
But a script is needed.



Also in regards to the path, is it starting from the folder where bitcoin.conf is located? or the root of the system? I suppose i should create a home folder where the bitcoin.conf is?

If you specify the path like directory1/test.sh then it is a relative path (from where the executable is being started started (e.g. bitcoind folder)).
If you specify the path like /directory1/test.sh then it is an absolute path (from root folder).



& to access the $1 in argv, do i simply do $transactionID = $argv[$1]; ?

IIRC you just have to use $transactionID = $1.

But i am not completely sure about that. It may also be $argv[1] or $argv[0](less probable, since this should be the command 'itself').
newbie
Activity: 5
Merit: 0
I'm running a Symfony4 (PHP) project which makes use of a routing system.

Routing system?  Huh Mind explaining what you mean?

You can read more here: https://symfony.com/doc/current/routing.html

It's essentially just removing the uglyness in URL's, such as the .php extension...



I want to use walletnotify in the following way, but i want to make sure that it's right before testing:

bitcoin.conf:

walletnotify=curl http://127.0.0.1:8000/wallet/notify/%s

~snip~

how would i use the argv function in this case and would it work?

You have to specify the path to your script.
E.g. :
Code:
walletnotify = /home/path/to/your/script.sh %s

And inside your script.sh you can have the following:
Code:
#!/bin/sh
curl http://127.0.0.1:8000/wallet/notify/$1

%s (transaction ID) will be passed to your script and can be used/accessed via $1 (argv).


Note that walletnotify is called when:
1) You receive a transaction
2) You send a transaction
3) A (receiving) transaction gets the first confirmation

If you want to check for a further status (2+ confirmations), you have to use blocknotify instead of walletnotify.





Thank you very much, i'll try it out! Is there no way to do it directly without the need for a bash script?

Also in regards to the path, is it starting from the folder where bitcoin.conf is located? or the root of the system? I suppose i should create a home folder where the bitcoin.conf is?

& to access the $1 in argv, do i simply do $transactionID = $argv[$1]; ?
legendary
Activity: 1624
Merit: 2481
I'm running a Symfony4 (PHP) project which makes use of a routing system.

Routing system?  Huh Mind explaining what you mean?



I want to use walletnotify in the following way, but i want to make sure that it's right before testing:

bitcoin.conf:

walletnotify=curl http://127.0.0.1:8000/wallet/notify/%s

~snip~

how would i use the argv function in this case and would it work?

You have to specify the path to your script.
E.g. :
Code:
walletnotify = /home/path/to/your/script.sh %s

And inside your script.sh you can have the following:
Code:
#!/bin/sh
curl http://127.0.0.1:8000/wallet/notify/$1

%s (transaction ID) will be passed to your script and can be used/accessed via $1 (argv).


Note that walletnotify is called when:
1) You receive a transaction
2) You send a transaction
3) A (receiving) transaction gets the first confirmation

If you want to check for a further status (2+ confirmations), you have to use blocknotify instead of walletnotify.



newbie
Activity: 5
Merit: 0
Hi.

I'm running a Symfony4 (PHP) project which makes use of a routing system.

I want to use walletnotify in the following way, but i want to make sure that it's right before testing:

bitcoin.conf:

walletnotify=curl http://127.0.0.1:8000/wallet/notify/%s


Route:

https://i.stack.imgur.com/nBDNl.png

how would i use the argv function in this case and would it work without specifying a .php extension?


Jump to: