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
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 ?
2
Answers
I’m not familiar with PHP, but you’ve asked:
Your
alert
message as it is currently written in thealert_message
argument, is not valid JSON and therefore it is sent as a txt/plain content type. If you want thealert
to be sent as application/json you will need to have it in a valid JSON.You can read more about it here.
EDIT:
You can make minor adjustments to your code, and it will be sent as JSON:
Replace the script with:
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
)