简体   繁体   中英

How to get webhook response data using tradingview and php

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?

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"}')

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM