简体   繁体   English

如何使用tradingview和php获取webhook响应数据

[英]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.我正在尝试使用 webhook 和交易视图警报将 Tradingview 信号发送到我的远程 php 脚本。 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然后我设置一个 PHP 脚本如下从交易视图接收 POST curl JSON 数据

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.我通过 email 正确收到警报,但我没有收到 /home/user/public_html/data.txt 中的数据。 What am I doing wrong?我究竟做错了什么? How to send the Tradingview JSON data to my remote PHP script?如何将 Tradingview JSON 数据发送到我的远程 PHP 脚本?

I'm not familiar with PHP, but you've asked:我不熟悉 PHP,但你问过:

How to send the Tradingview JSON data to my remote PHP script?如何将 Tradingview JSON 数据发送到我的远程 PHP 脚本?

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.当前在alert_message参数中写入的alert消息无效 JSON,因此它以 txt/plain 内容类型发送。 If you want the alert to be sent as application/json you will need to have it in a valid JSON.如果您希望将alert作为 application/json 发送,您需要将其包含在有效的 JSON 中。

Webhooks allow you to send a POST request to a certain URL every time the alert is triggered. Webhooks 允许您在每次触发警报时向某个 URL 发送 POST 请求。 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.为您的应用添加正确的 URL,我们将在触发警报后立即发送 POST 请求,并在请求正文中包含警报消息。 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.如果警报消息有效 JSON,我们将发送内容类型为“application/json”的请求 header。否则,我们将发送内容类型为“text/plain”的请求 header。

You can read more about it here .您可以在此处阅读更多相关信息。

EDIT: You can make minor adjustments to your code, and it will be sent as JSON:编辑:您可以对您的代码进行细微调整,它将作为 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).也许数据存在于$_POST变量中,如果没有,请在此处发布整个结果(通过 pastebin)。

(also make sure the PHP script starts with <?php ) (还要确保 PHP 脚本以<?php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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