繁体   English   中英

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

[英]How to get webhook response data using tradingview and php

我正在尝试使用 webhook 和交易视图警报将 Tradingview 信号发送到我的远程 php 脚本。 我不能成功。 我正在走这条路

在我的交易观点策略中,我有这个

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

然后我将警报设置如下

交易视图警报

然后我设置一个 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);   
}

我通过 email 正确收到警报,但我没有收到 /home/user/public_html/data.txt 中的数据。 我究竟做错了什么? 如何将 Tradingview JSON 数据发送到我的远程 PHP 脚本?

我不熟悉 PHP,但你问过:

如何将 Tradingview JSON 数据发送到我的远程 PHP 脚本?

当前在alert_message参数中写入的alert消息无效 JSON,因此它以 txt/plain 内容类型发送。 如果您希望将alert作为 application/json 发送,您需要将其包含在有效的 JSON 中。

Webhooks 允许您在每次触发警报时向某个 URL 发送 POST 请求。 创建或编辑警报时可以启用此功能。 为您的应用添加正确的 URL,我们将在触发警报后立即发送 POST 请求,并在请求正文中包含警报消息。 如果警报消息有效 JSON,我们将发送内容类型为“application/json”的请求 header。否则,我们将发送内容类型为“text/plain”的请求 header。

您可以在此处阅读更多相关信息。

编辑:您可以对您的代码进行细微调整,它将作为 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"}')

将脚本替换为:

<?php

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

更好地理解传入的数据。

也许数据存在于$_POST变量中,如果没有,请在此处发布整个结果(通过 pastebin)。

(还要确保 PHP 脚本以<?php

暂无
暂无

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

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