Bitcoin Forum
May 13, 2024, 08:46:13 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Wallet notify using a PHP routing system?  (Read 243 times)
StayCool93 (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 0


View Profile
May 15, 2018, 06:43:17 AM
Last edit: May 15, 2018, 07:20:20 AM by StayCool93
 #1

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?


1715589973
Hero Member
*
Offline Offline

Posts: 1715589973

View Profile Personal Message (Offline)

Ignore
1715589973
Reply with quote  #2

1715589973
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715589973
Hero Member
*
Offline Offline

Posts: 1715589973

View Profile Personal Message (Offline)

Ignore
1715589973
Reply with quote  #2

1715589973
Report to moderator
1715589973
Hero Member
*
Offline Offline

Posts: 1715589973

View Profile Personal Message (Offline)

Ignore
1715589973
Reply with quote  #2

1715589973
Report to moderator
1715589973
Hero Member
*
Offline Offline

Posts: 1715589973

View Profile Personal Message (Offline)

Ignore
1715589973
Reply with quote  #2

1715589973
Report to moderator
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
May 15, 2018, 07:26:09 AM
 #2

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.




StayCool93 (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 0


View Profile
May 15, 2018, 07:29:53 AM
Last edit: May 15, 2018, 07:58:02 AM by StayCool93
 #3

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]; ?
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
May 15, 2018, 08:05:47 AM
 #4

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').

starmyc
Full Member
***
Offline Offline

Activity: 198
Merit: 130

Some random software engineer


View Profile
May 15, 2018, 09:40:36 AM
 #5

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.

Hi, I'm just some random software engineer.
You can check my projects: Bitcoin & altcoin balances/addresses listing dumps: https://balances.crypto-nerdz.org/
StayCool93 (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 0


View Profile
May 15, 2018, 10:04:56 AM
 #6

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.
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
May 15, 2018, 10:11:57 AM
 #7

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.

StayCool93 (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 0


View Profile
May 15, 2018, 10:24:35 AM
 #8

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...
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
May 15, 2018, 10:43:30 AM
 #9

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.

starmyc
Full Member
***
Offline Offline

Activity: 198
Merit: 130

Some random software engineer


View Profile
May 15, 2018, 10:46:15 AM
Last edit: May 15, 2018, 04:38:56 PM by starmyc
 #10

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:
<?php

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:
<?php

if(count($argv) != 2) {
  echo 
"Usage: " $argv[0] . " <txid>\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

Hi, I'm just some random software engineer.
You can check my projects: Bitcoin & altcoin balances/addresses listing dumps: https://balances.crypto-nerdz.org/
StayCool93 (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 0


View Profile
May 15, 2018, 03:35:39 PM
 #11

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.
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!