2

I'm trying to get Tradingview signals to my remote php script using webhook and trading view alerts. I can't succeed . I'm following this way

In my trading view strategy I have this

strategy.entry("Long", strategy.long, alert_message = "entry_long")
strategy.exit("Exit Long", "Long", limit=LONG_take_profit, stop=LONG_stop_loss, alert_message = "exit_long")

then I set the alert as follow

trading view alert

Then I set a PHP script as follow to receive the POST curl JSON data from Trading view

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // fetch RAW input
    $json = file_get_contents('php://input');

    // decode json
    $object = json_decode($json);

    // expecting valid json
    if (json_last_error() !== JSON_ERROR_NONE) {
        die(header('HTTP/1.0 415 Unsupported Media Type'));
    }

    $servdate2 = time();
    $servdate=date('d-M-Y H:i:s',$servdate2);    
    file_put_contents("/home/user/public_html/data.txt", "$servdate :".print_r($object, true),FILE_APPEND);   
}

I receive the alert via email correctly but I do not receive data in /home/user/public_html/data.txt . What am I doing wrong ? How to send the Tradingview JSON data to my remote PHP script ?

gr68
  • 420
  • 1
  • 10
  • 25
  • Is the webhook reaching your server, can you see the hits in your Apache/webserver logs? Have you tried just logging what the incoming payload looks like, eg writing `$json` to a file? Anything in your PHP logs? – Don't Panic Nov 03 '22 at 22:22
  • The issue here might be access control on the webserver level. maybe check that first if your print_r isn't dumping anything – Moudi Nov 04 '22 at 07:49
  • 1
    @Moudi if by "*access control*" you mean permissions, OP [has already confirmed](https://stackoverflow.com/questions/74218127/how-to-get-webhook-response-data-using-tradingview-and-php#comment131172753_74259994) in comments that PHP/webserver can write the file. – Don't Panic Nov 04 '22 at 08:11
  • @gr68 Is the webhook reaching your server, can you see the hits in your Apache/webserver logs? Have you tried just logging what the incoming payload looks like, eg writing $json to a file? – Don't Panic Nov 05 '22 at 23:18

2 Answers2

0

I'm not familiar with PHP, but you've asked:

How to send the Tradingview JSON data to my remote PHP script ?

Your alert message as it is currently written in the alert_message argument, is not valid JSON and therefore it is sent as a txt/plain content type. If you want the alert to be sent as application/json you will need to have it in a valid JSON.

Webhooks allow you to send a POST request to a certain URL every time the alert is triggered. This feature can be enabled when you create or edit an alert. Add the correct URL for your app and we will send a POST request as soon as the alert is triggered, with the alert message in the body of the request. If the alert message is valid JSON, we will send a request with an "application/json" content-type header. Otherwise, we will send "text/plain" as a content-type header.

You can read more about it here.

EDIT: You can make minor adjustments to your code, and it will be sent as JSON:

strategy.entry("Long", strategy.long, alert_message = '{"message": "entry_long"}')
strategy.exit("Exit Long", "Long", limit=LONG_take_profit, stop=LONG_stop_loss, alert_message = '{"message": "exit_long"}')
mr_statler
  • 1,913
  • 2
  • 3
  • 14
  • Thank you, please can you explain how should I change my code to send JSON correctly ? – gr68 Oct 31 '22 at 12:22
  • I've edited my answer and added an example based on your code – mr_statler Oct 31 '22 at 12:36
  • thank you , I'm trying it and report you. – gr68 Oct 31 '22 at 13:21
  • it does not work , nothing is stored in file. – gr68 Nov 01 '22 at 14:40
  • If JSON is passing through to your server (you can check this easily using apps like Insomnia), then it might be a PHP issue. Maybe try to edit your question and add PHP label to it so that the PHP community could help – mr_statler Nov 03 '22 at 07:03
  • I tried to send a JSON to my site and the php script above worked correctly saving the data on file data.txt . So there should not be problem with php script. – gr68 Nov 03 '22 at 07:16
  • Just to review - alerts are sent (you get an email each time), they are sent as JSON (they should. I use it all the time with that syntax), they are sent to your site (maybe double check this) and if your site gets a different JSON it works perfectly, but it still doesn't work? – mr_statler Nov 03 '22 at 07:40
  • Exactly , I confirm it – gr68 Nov 03 '22 at 08:07
0

Replace the script with:

<?php

file_put_contents("/home/user/public_html/data.txt", print_r([$_GET, $_POST, $_SERVER], true));

to better understand the incoming data.

Maybe the data are present in $_POST variable, if not, post the whole result here (via pastebin).

(also make sure the PHP script starts with <?php)

mvorisek
  • 3,290
  • 2
  • 18
  • 53
  • I tried, nothing is stored in file using this file . In addition to trading view alert I tried also curl -H 'Content-Type: application/json; charset=utf-8' -d '{"text": "alert"}' -X POST https://example.com/file.php (sent from another server) and nothing is stored. – gr68 Nov 03 '22 at 14:08
  • you can try `__DIR__ . '/data.txt'` path instead, it should store file called `data.txt` inside the script directory, if this will also store nothing - is the file ever created? - then check if the directory is writeable/owned by the webserver user. – mvorisek Nov 03 '22 at 16:41